Skip to content

Commit ec33ac3

Browse files
SahilSainiYMLMark Pospesel
andauthored
[CM-1317] add rendering mode support (#60)
* [ISSUE] add rendering mode support * [UPDATE] added lint rule as in new swiftLint we get warnings even for disabled rules for file [UPDATE] add disable rule * [UPDATE] reenabled function length rule [UPDATE] added // swiftlint:disable superfluous_disable_command * [UPDATE] review comments resolved * [UPDATE] rendering default is .automatic * Update UIView+constrainEdgesTests.swift * Update UIView+constrainEdgesTests.swift * [CM-1317] Update documentation and unit tests --------- Co-authored-by: Mark Pospesel <[email protected]>
1 parent 962c919 commit ec33ac3

File tree

6 files changed

+54
-38
lines changed

6 files changed

+54
-38
lines changed

Sources/YCoreUI/Extensions/UIKit/UIColor+rgbComponents.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ public extension UIColor {
2929
return (red, green, blue, alpha)
3030
}
3131
}
32+
// swiftlint: enable large_tuple

Sources/YCoreUI/Protocols/SystemImage.swift

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,29 @@ import UIKit
1414
/// to `SystemImage`. The raw value of the enum should match a sytem image name (e.g. `checkmark.seal`).
1515
public protocol SystemImage: RawRepresentable where RawValue == String {
1616
/// Fallback image to use in case a system image cannot be loaded.
17-
/// (default is a 16 x 16 square filled with `.systemPink`)
1817
static var fallbackImage: UIImage { get }
1918

2019
/// A system image for this name value.
21-
///
22-
/// Default implementation calls `loadImage` and nil-coalesces to `fallbackImage`.
2320
var image: UIImage { get }
2421

2522
/// Image will scale according to the specified text style.
2623
///
27-
/// Default implementation is `.body`.
24+
/// Return `nil` to not have the system image scale (not recommended).
2825
static var textStyle: UIFont.TextStyle? { get }
2926

30-
/// Image configuration to be used in `loadImage()`.
31-
///
32-
/// Default implementation is `UIImage.SymbolConfiguration(textStyle: textStyle)`.
33-
/// Returns `nil` when `textStyle` is `nil`.
27+
/// Image configuration to be used to load the system image.
3428
static var configuration: UIImage.Configuration? { get }
3529

30+
/// Rendering mode to use for the system image.
31+
static var renderingMode: UIImage.RenderingMode { get }
32+
3633
/// Loads the named system image.
3734
/// - Returns: The named system image or else `nil` if the system image cannot be loaded.
3835
func loadImage() -> UIImage?
3936
}
4037

4138
extension SystemImage {
42-
/// Image will scale according to the specified text style.
43-
public static var textStyle: UIFont.TextStyle? { .body }
44-
45-
/// Image configuration to be used in `loadImage()`.
46-
///
47-
/// Returns `nil` when `textStyle` is `nil`.
48-
public static var configuration: UIImage.Configuration? {
49-
guard let textStyle = textStyle else {
50-
return nil
51-
}
52-
return UIImage.SymbolConfiguration(textStyle: textStyle)
53-
}
54-
55-
/// Fallback image to use in case a system image cannot be loaded.
56-
/// (default is a 16 x 16 square filled with `.systemPink`)
39+
/// Returns a 16 x 16 square filled with `.systemPink`.
5740
public static var fallbackImage: UIImage {
5841
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 16, height: 16))
5942
let image = renderer.image { ctx in
@@ -63,17 +46,28 @@ extension SystemImage {
6346
return image
6447
}
6548

66-
/// Loads the named system image.
67-
///
68-
/// Default implementation uses `UIImage(systemName:)` passing in the associated `rawValue`.
69-
/// - Returns: The named system image or else `nil` if the system image cannot be loaded.
49+
/// Returns `.body` text style.
50+
public static var textStyle: UIFont.TextStyle? { .body }
51+
52+
/// Returns `UIImage.SymbolConfiguration(textStyle:)`
53+
/// passing in the specified `textStyle` or else returns `nil` if `textStyle` is `nil`.
54+
public static var configuration: UIImage.Configuration? {
55+
guard let textStyle = textStyle else {
56+
return nil
57+
}
58+
return UIImage.SymbolConfiguration(textStyle: textStyle)
59+
}
60+
61+
/// Returns `.automatic` rendering mode.
62+
public static var renderingMode: UIImage.RenderingMode { .automatic }
63+
64+
/// Returns `UIImage(systemName:)` passing in the associated `rawValue` and `configuration`
65+
/// and combined with the specified `renderingMode`.
7066
public func loadImage() -> UIImage? {
71-
UIImage(systemName: rawValue, withConfiguration: Self.configuration)
67+
UIImage(systemName: rawValue, withConfiguration: Self.configuration)?.withRenderingMode(Self.renderingMode)
7268
}
7369

74-
/// A system image for this name value.
75-
///
76-
/// Default implementation calls `loadImage` and nil-coalesces to `fallbackImage`.
70+
/// Returns `loadImage()` nil-coalesced to `fallbackImage`.
7771
public var image: UIImage {
7872
guard let image = loadImage() else {
7973
if YCoreUI.isLoggingEnabled {

Tests/YCoreUITests/Extensions/Foundation/CGFloat+roundedTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import XCTest
1010
@testable import YCoreUI
1111

1212
// Large tuples help us build unit test expectations concisely
13-
// swiftlint:disable large_tuple
13+
// swiftlint: disable large_tuple
1414

1515
final class CGFloatRoundedTests: XCTestCase {
1616
typealias ScalingInputs = (
@@ -75,3 +75,4 @@ final class CGFloatRoundedTests: XCTestCase {
7575
}
7676
}
7777
}
78+
// swiftlint: enable large_tuple

Tests/YCoreUITests/Extensions/UIKit/UIColor+WCAGTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import XCTest
1010
@testable import YCoreUI
1111

1212
// Large tuples help us build unit test expectations concisely
13-
// swiftlint:disable large_tuple
13+
// swiftlint: disable large_tuple
1414

1515
final class UIColorWCAGTests: XCTestCase {
1616
typealias ColorInputs = (foreground: UIColor, background: UIColor, context: WCAGContext)
@@ -183,3 +183,4 @@ final class UIColorWCAGTests: XCTestCase {
183183
XCTAssertEqual(round(ratio1 * 100) / 100, round(ratio2 * 100) / 100)
184184
}
185185
}
186+
// swiftlint: enable large_tuple

Tests/YCoreUITests/Extensions/UIKit/UIColor+rgbValueTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import XCTest
1010
@testable import YCoreUI
1111

1212
// Large tuples help us build unit test expectations concisely
13-
// swiftlint:disable large_tuple
13+
// swiftlint: disable large_tuple
1414

1515
final class UIColorRgbValueTests: XCTestCase {
1616
typealias ColorTest = (color: UIColor, prefix: String?, isUppercase: Bool, output: String)
@@ -75,3 +75,4 @@ final class UIColorRgbValueTests: XCTestCase {
7575
YCoreUI.isLoggingEnabled = true
7676
}
7777
}
78+
// swiftlint: enable large_tuple

Tests/YCoreUITests/Protocols/SystemImageTests.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ final class SystemImageTests: XCTestCase {
3232

3333
YCoreUI.isLoggingEnabled = true
3434
}
35-
35+
36+
func test_systemImage_deliversDefaultFallback() {
37+
XCTAssertEqual(DefaultSymbols.defaultCase.image.pngData(), DefaultSymbols.fallbackImage.pngData())
38+
}
39+
3640
func test_defaultImageScaling() {
3741
XCTAssertEqual(Symbols.textStyle, .body)
3842
XCTAssertEqual(Symbols.configuration, UIImage.SymbolConfiguration(textStyle: .body))
@@ -47,9 +51,15 @@ final class SystemImageTests: XCTestCase {
4751
XCTAssertEqual(SymbolCustomScaling.textStyle, .title1)
4852
XCTAssertEqual(SymbolCustomScaling.configuration, UIImage.SymbolConfiguration(textStyle: .title1))
4953
}
50-
51-
func test_systemImage_deliversDefaultFallback() {
52-
XCTAssertEqual(DefaultSymbols.defaultCase.image.pngData(), DefaultSymbols.fallbackImage.pngData())
54+
55+
func test_defaultRenderingMode() {
56+
XCTAssertEqual(Symbols.renderingMode, .automatic)
57+
}
58+
59+
func test_customRenderingMode() {
60+
SymbolCustomRenderingMode.allCases.forEach {
61+
XCTAssertEqual($0.image.renderingMode, .alwaysOriginal)
62+
}
5363
}
5464
}
5565

@@ -88,4 +98,12 @@ extension SystemImageTests {
8898

8999
static var textStyle: UIFont.TextStyle? { .title1 }
90100
}
101+
102+
enum SymbolCustomRenderingMode: String, CaseIterable, SystemImage {
103+
case plus
104+
case minus
105+
case trash
106+
107+
static var renderingMode: UIImage.RenderingMode { .alwaysOriginal }
108+
}
91109
}

0 commit comments

Comments
 (0)