Skip to content

[Issue-50] Support scalable system image. #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ Easily load system images (SF Symbols) from any string-based `Enum`. All you nee

Why bother doing this when it just wraps `UIImage(systemName:)`? Because
1. `UIImage(systemName:)` returns `UIImage?` while `SystemImage.image` returns `UIImage`.
2. Organizing your system images into enums encourages better architecture (and helps avoid stringly-typed errors).
3. Easier to unit test.
2. By default `SystemImage.image` returns images that scale with Dynamic Type.
3. Organizing your system images into enums encourages better architecture (and helps avoid stringly-typed errors).
4. Easier to unit test.

```swift
// Conform your Enum to SystemImage
Expand Down
26 changes: 25 additions & 1 deletion Sources/YCoreUI/Protocols/SystemImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,37 @@ public protocol SystemImage: RawRepresentable where RawValue == String {
///
/// Default implementation calls `loadImage` and nil-coalesces to `fallbackImage`.
var image: UIImage { get }

/// Image will scale according to the specified text style.
///
/// Default implementation is `.body`.
static var textStyle: UIFont.TextStyle? { get }

/// Image configuration to be used in `loadImage()`.
///
/// Default implementation is `UIImage.SymbolConfiguration(textStyle: textStyle)`.
/// Returns `nil` when `textStyle` is `nil`.
static var configuration: UIImage.Configuration? { get }

/// Loads the named system image.
/// - Returns: The named system image or else `nil` if the system image cannot be loaded.
func loadImage() -> UIImage?
}

extension SystemImage {
/// Image will scale according to the specified text style.
public static var textStyle: UIFont.TextStyle? { .body }

/// Image configuration to be used in `loadImage()`.
///
/// Returns `nil` when `textStyle` is `nil`.
public static var configuration: UIImage.Configuration? {
guard let textStyle = textStyle else {
return nil
}
return UIImage.SymbolConfiguration(textStyle: textStyle)
}

/// Fallback image to use in case a system image cannot be loaded.
/// (default is a 16 x 16 square filled with `.systemPink`)
public static var fallbackImage: UIImage {
Expand All @@ -44,7 +68,7 @@ extension SystemImage {
/// Default implementation uses `UIImage(systemName:)` passing in the associated `rawValue`.
/// - Returns: The named system image or else `nil` if the system image cannot be loaded.
public func loadImage() -> UIImage? {
UIImage(systemName: rawValue)
UIImage(systemName: rawValue, withConfiguration: Self.configuration)
}

/// A system image for this name value.
Expand Down
31 changes: 30 additions & 1 deletion Tests/YCoreUITests/Protocols/SystemImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,22 @@ final class SystemImageTests: XCTestCase {

YCoreUI.isLoggingEnabled = true
}


func test_defaultImageScaling() {
XCTAssertEqual(Symbols.textStyle, .body)
XCTAssertEqual(Symbols.configuration, UIImage.SymbolConfiguration(textStyle: .body))
}

func test_imageWithoutScaling() {
XCTAssertNil(SymbolWithoutScaling.textStyle)
XCTAssertNil(SymbolWithoutScaling.configuration)
}

func test_customImageScaling() {
XCTAssertEqual(SymbolCustomScaling.textStyle, .title1)
XCTAssertEqual(SymbolCustomScaling.configuration, UIImage.SymbolConfiguration(textStyle: .title1))
}

func test_systemImage_deliversDefaultFallback() {
XCTAssertEqual(DefaultSymbols.defaultCase.image.pngData(), DefaultSymbols.fallbackImage.pngData())
}
Expand All @@ -59,4 +74,18 @@ extension SystemImageTests {
enum DefaultSymbols: String, CaseIterable, SystemImage {
case defaultCase
}

enum SymbolWithoutScaling: String, CaseIterable, SystemImage {
case checked = "checkmark.square"
case unchecked = "square"

static var textStyle: UIFont.TextStyle? { nil }
}

enum SymbolCustomScaling: String, CaseIterable, SystemImage {
case checked = "checkmark.square"
case unchecked = "square"

static var textStyle: UIFont.TextStyle? { .title1 }
}
}