File tree Expand file tree Collapse file tree 5 files changed +64
-10
lines changed
Examples/DemosApp/DemosApp
Assets.xcassets/avatar_placeholder.imageset
ComponentsPreview/PreviewPages Expand file tree Collapse file tree 5 files changed +64
-10
lines changed Original file line number Diff line number Diff line change 1717 "info" : {
1818 "author" : " xcode" ,
1919 "version" : 1
20- },
21- "properties" : {
22- "template-rendering-intent" : " template"
2320 }
2421}
Original file line number Diff line number Diff line change @@ -35,6 +35,11 @@ struct ButtonPreview: View {
3535 Text ( " Leading " ) . tag ( ButtonVM . ImageLocation. leading)
3636 Text ( " Trailing " ) . tag ( ButtonVM . ImageLocation. trailing)
3737 }
38+ Picker ( " Image Rendering Mode " , selection: self . $model. imageRenderingMode) {
39+ Text ( " Default " ) . tag ( Optional< ImageRenderingMode> . none)
40+ Text ( " Template " ) . tag ( ImageRenderingMode . template)
41+ Text ( " Original " ) . tag ( ImageRenderingMode . original)
42+ }
3843 Picker ( " Image Source " , selection: self . $model. imageSrc) {
3944 Text ( " SF Symbol " ) . tag ( ButtonVM . ImageSource. sfSymbol ( " camera.fill " ) )
4045 Text ( " Local " ) . tag ( ButtonVM . ImageSource. local ( " avatar_placeholder " ) )
Original file line number Diff line number Diff line change 11import Foundation
22
3- /// Defines the image source options for a button.
43extension ButtonVM {
4+ /// Defines the image source options for a button.
55 public enum ImageSource : Hashable {
66 /// An image loaded from a system SF Symbol.
77 ///
Original file line number Diff line number Diff line change @@ -30,6 +30,9 @@ public struct ButtonVM: ComponentVM {
3030 /// Defaults to `.leading`.
3131 public var imageLocation : ImageLocation = . leading
3232
33+ /// Defines how image is rendered.
34+ public var imageRenderingMode : ImageRenderingMode ?
35+
3336 /// The source of the image to be displayed.
3437 public var imageSrc : ImageSource ?
3538
@@ -184,15 +187,14 @@ extension ButtonVM {
184187extension ButtonVM {
185188 var image : UIImage ? {
186189 guard let imageSrc else { return nil }
187- switch imageSrc {
190+
191+ let image = switch imageSrc {
188192 case . sfSymbol( let name) :
189- return UIImage ( systemName: name) ? . withTintColor (
190- self . foregroundColor. uiColor,
191- renderingMode: . alwaysOriginal
192- )
193+ UIImage ( systemName: name)
193194 case . local( let name, let bundle) :
194- return UIImage ( named: name, in: bundle, compatibleWith: nil )
195+ UIImage ( named: name, in: bundle, compatibleWith: nil )
195196 }
197+ return image? . withRenderingMode ( self . imageRenderingMode)
196198 }
197199}
198200
Original file line number Diff line number Diff line change 1+ import SwiftUI
2+ import UIKit
3+
4+ /// A type that indicates how images are rendered.
5+ public enum ImageRenderingMode {
6+ /// A mode that renders all non-transparent pixels as the foreground
7+ /// color.
8+ case template
9+ /// A mode that renders pixels of bitmap images as-is.
10+ ///
11+ /// For system images created from the SF Symbol set, multicolor symbols
12+ /// respect the current foreground and accent colors.
13+ case original
14+ }
15+
16+ // MARK: - UIKit Helpers
17+
18+ extension ImageRenderingMode {
19+ var uiImageRenderingMode : UIImage . RenderingMode {
20+ switch self {
21+ case . template:
22+ return . alwaysTemplate
23+ case . original:
24+ return . alwaysOriginal
25+ }
26+ }
27+ }
28+
29+ extension UIImage {
30+ func withRenderingMode( _ mode: ImageRenderingMode ? ) -> UIImage {
31+ if let mode {
32+ return self . withRenderingMode ( mode. uiImageRenderingMode)
33+ } else {
34+ return self
35+ }
36+ }
37+ }
38+
39+ // MARK: - SwiftUI Helpers
40+
41+ extension ImageRenderingMode {
42+ var imageRenderingModel : Image . TemplateRenderingMode {
43+ switch self {
44+ case . template:
45+ return . template
46+ case . original:
47+ return . original
48+ }
49+ }
50+ }
You can’t perform that action at this time.
0 commit comments