Skip to content

Commit

Permalink
Add resizable + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
divadretlaw committed Feb 27, 2022
1 parent d162980 commit 1134a01
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 27 deletions.
12 changes: 12 additions & 0 deletions Classes/Extensions/Amazing+Tools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public extension Amazing {
}
}

#if canImport(SwiftUI)
import SwiftUI

@available(iOS 13, macOS 11, watchOS 6, tvOS 13, *)
public extension Amazing {
/// A SwiftUI Image of the icon
var image: Awesome.Image<Self> {
Awesome.Image(icon: self)
}
}
#endif

extension Amazing {

/// Returns an unique identifier string that contains the font name, font style and icon name
Expand Down
4 changes: 2 additions & 2 deletions Classes/Libs/Crossplatform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

#if os(iOS) || os(watchOS) || os(tvOS)

import UIKit
import UIKit

extension Amazing {
public typealias Color = UIColor
Expand All @@ -19,7 +19,7 @@ extension Amazing {

#elseif os(OSX)

import Cocoa
import Cocoa

extension Amazing {
public typealias Color = NSColor
Expand Down
87 changes: 63 additions & 24 deletions Classes/SwiftUI/Awesome+Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,23 @@
#if canImport(SwiftUI)
import SwiftUI

public extension Amazing {
@available(iOS 13, macOS 11, watchOS 6, tvOS 13, *)
var image: Awesome.Image<Self> {
Awesome.Image(icon: self)
}
}

@available(iOS 13, macOS 11, watchOS 6, tvOS 13, *)
extension Awesome {
@available(iOS 13, macOS 11, watchOS 6, tvOS 13, *)
/// A view wrapper around an Awesome.Icon
public struct Image<Icon>: View where Icon: Amazing {
/// 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

var icon: Icon

private var forcedSize: CGSize?
private var color: Amazing.Color
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
Expand All @@ -39,55 +36,97 @@ extension Awesome {
}

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: color, backgroundColor: backgroundColor))
SwiftUI.Image(uiImage: icon.asImage(size: size, color: foregroundColor, backgroundColor: backgroundColor))
#elseif os(macOS)
SwiftUI.Image(nsImage: icon.asImage(size: size, color: color, backgroundColor: backgroundColor))
SwiftUI.Image(nsImage: icon.asImage(size: size, color: foregroundColor, backgroundColor: backgroundColor))
#endif
}

public init(icon: Icon) {
/// 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.color = .black
self.foregroundColor = .black
self.backgroundColor = .clear

self.isResizable = false
self.capInsets = EdgeInsets()
self.resizingMode = .stretch
}

/// Changes the size of the underlying image
/// 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
/// 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
}

/// Changes the foreground color of the underlying image
/// Sets the color of the icon.
///
/// - Parameter color: The color
/// - 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.color = color
view.foregroundColor = color
return view
}

/// Changes the background color of the underlying image
/// Sets the background color of the icon.
///
/// - Parameter color: The background color to use when displaying this icon.
///
/// - Parameter color: The background color
/// - 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
2 changes: 1 addition & 1 deletion Classes/SwiftUI/SwiftUI+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import SwiftUI

@available(iOS 13, macOS 11, watchOS 6, tvOS 13, *)
extension SwiftUI.Font {
extension Font {
var textStyle: Amazing.Font.TextStyle? {
switch self {
case .largeTitle:
Expand Down

0 comments on commit 1134a01

Please sign in to comment.