-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from LiveUI/swiftui
SwiftUI
- Loading branch information
Showing
7 changed files
with
226 additions
and
4 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
Classes/Extensions/Image+Awesome.swift → Classes/Extensions/Amazing+Image.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// Image+Awesome.swift | ||
// Amazing+Image.swift | ||
// Awesome | ||
// | ||
// Created by Ondrej Rafaj on 13/10/2017. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// | ||
// Awesome+Image.swift | ||
// Awesome | ||
// | ||
// Created by David Walter on 22.02.22. | ||
// | ||
|
||
#if canImport(SwiftUI) | ||
import SwiftUI | ||
|
||
@available(iOS 13, macOS 11, watchOS 6, tvOS 13, *) | ||
extension Awesome { | ||
/// A view wrapper around an Awesome.Image | ||
public struct Image<AmazingType>: View where AmazingType: Amazing { | ||
var icon: AmazingType | ||
|
||
@Environment(\.imageScale) var imageScale | ||
@Environment(\.font) var font | ||
|
||
private var forcedSize: CGSize? | ||
private var foregroundColor: Amazing.Color | ||
private var backgroundColor: Amazing.Color | ||
|
||
private var isResizable: Bool | ||
private var capInsets: EdgeInsets | ||
private var resizingMode: SwiftUI.Image.ResizingMode | ||
|
||
var size: CGSize { | ||
if let size = forcedSize { | ||
return size | ||
} | ||
|
||
let textStyle = self.font?.textStyle ?? .body | ||
let size = Amazing.Font.preferredFont(forTextStyle: textStyle).pointSize * imageScale.value | ||
return CGSize(width: size * 1.28571429, height: size) | ||
} | ||
|
||
public var body: some View { | ||
if isResizable { | ||
image.resizable(capInsets: capInsets, resizingMode: resizingMode) | ||
} else { | ||
image | ||
} | ||
} | ||
|
||
var image: SwiftUI.Image { | ||
#if os(iOS) || os(watchOS) || os(tvOS) | ||
SwiftUI.Image(uiImage: icon.asImage(size: size, color: foregroundColor, backgroundColor: backgroundColor)) | ||
#elseif os(macOS) | ||
SwiftUI.Image(nsImage: icon.asImage(size: size, color: foregroundColor, backgroundColor: backgroundColor)) | ||
#endif | ||
} | ||
|
||
/// Creates a Font Awesome icon image. | ||
/// | ||
/// This initializer creates an image using a Font Awesome icon. | ||
/// | ||
/// - Parameters: | ||
/// - icon: The icon to use | ||
public init(icon: AmazingType) { | ||
self.icon = icon | ||
|
||
self.forcedSize = nil | ||
self.foregroundColor = .black | ||
self.backgroundColor = .clear | ||
|
||
self.isResizable = false | ||
self.capInsets = EdgeInsets() | ||
self.resizingMode = .stretch | ||
} | ||
|
||
/// Changes the size of the underlying image. This will disable the automatic sizing based on the font | ||
/// | ||
/// - Parameter size: The size to change to | ||
/// | ||
/// - Returns: An image that uses the size you supply | ||
public func size(_ size: CGFloat) -> Self { | ||
var view = self | ||
view.forcedSize = CGSize(width: size, height: size) | ||
return view | ||
} | ||
|
||
/// Changes the size of the underlying image. This will disable the automatic sizing based on the font | ||
/// | ||
/// - Parameter size: The size to change to | ||
/// | ||
/// - Returns: An image that uses the size you supply | ||
public func size(_ size: CGSize) -> Self { | ||
var view = self | ||
view.forcedSize = size | ||
return view | ||
} | ||
|
||
/// Sets the color of the icon. | ||
/// | ||
/// - Parameter color: The foreground color to use when displaying this icon. | ||
/// | ||
/// - Returns: A view that uses the foreground color you supply. | ||
public func foregroundColor(_ color: Amazing.Color) -> Self { | ||
var view = self | ||
view.foregroundColor = color | ||
return view | ||
} | ||
|
||
/// Sets the background color of the icon. | ||
/// | ||
/// - Parameter color: The background color to use when displaying this icon. | ||
/// | ||
/// - Returns: An image that uses the background color you supply | ||
public func backgroundColor(_ color: Amazing.Color) -> Self { | ||
var view = self | ||
view.backgroundColor = color | ||
return view | ||
} | ||
|
||
/// Sets the mode by which SwiftUI resizes an image to fit its space. | ||
/// | ||
/// - Parameters: | ||
/// - capInsets: Inset values that indicate a portion of the image that SwiftUI doesn't resize. | ||
/// - resizingMode: The mode by which SwiftUI resizes the image. | ||
/// | ||
/// - Returns: An image, with the new resizing behavior set. | ||
public func resizable(capInsets: EdgeInsets = EdgeInsets(), resizingMode: SwiftUI.Image.ResizingMode = .stretch) -> Self { | ||
var view = self | ||
view.isResizable = true | ||
view.capInsets = capInsets | ||
view.resizingMode = resizingMode | ||
return view | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// SwiftUI+Extensions.swift | ||
// Awesome | ||
// | ||
// Created by David Walter on 23.02.22. | ||
// | ||
|
||
#if canImport(SwiftUI) | ||
import SwiftUI | ||
|
||
@available(iOS 13, macOS 11, watchOS 6, tvOS 13, *) | ||
extension Font { | ||
var textStyle: Amazing.Font.TextStyle? { | ||
switch self { | ||
case .largeTitle: | ||
#if os(tvOS) | ||
return nil | ||
#else | ||
return .largeTitle | ||
#endif | ||
case .title: | ||
return .title1 | ||
case .title: | ||
return .title2 | ||
case .title: | ||
return .title3 | ||
case .headline: | ||
return .headline | ||
case .subheadline: | ||
return .subheadline | ||
case .body: | ||
return .body | ||
case .callout: | ||
return .callout | ||
case .footnote: | ||
return .footnote | ||
case .caption: | ||
return .caption1 | ||
case .caption: | ||
return .caption2 | ||
default: | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
@available(iOS 13, macOS 11, watchOS 6, tvOS 13, *) | ||
extension Image.Scale { | ||
var value: CGFloat { | ||
switch self { | ||
case .small: | ||
return 1 / 1.28571429 | ||
case .medium: | ||
return 1 | ||
case .large: | ||
return 1.28571429 | ||
@unknown default: | ||
return 1 | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters