From 15760e3a41cce1de2be8fb506dbcbd8b68298658 Mon Sep 17 00:00:00 2001 From: Maxim Bunkov Date: Sun, 11 Oct 2020 13:10:56 +0500 Subject: [PATCH 1/2] feat(teamplates): update templates --- Templates/ColorStyles.stencil | 2 +- Templates/TextStyles.stencil | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Templates/ColorStyles.stencil b/Templates/ColorStyles.stencil index a2c12b8..7d0391a 100644 --- a/Templates/ColorStyles.stencil +++ b/Templates/ColorStyles.stencil @@ -35,7 +35,7 @@ import AppKit green: {{ style.color.green }}, blue: {{ style.color.blue }}, alpha: {{ style.color.alpha }} - ) + ).color {% endfor %} // MARK: - Instance Properties diff --git a/Templates/TextStyles.stencil b/Templates/TextStyles.stencil index a19314e..ab9de95 100644 --- a/Templates/TextStyles.stencil +++ b/Templates/TextStyles.stencil @@ -63,7 +63,7 @@ import AppKit /// Line height: {{ style.lineHeight|default:"default" }} /// Letter spacing: {{ style.letterSpacing|default:"default" }} {{ accessModifier }} static let {% call propertyName style.name %} = {{ styleTypeName }}( - font: {{ fontTypeName }}(name: "{{ style.font.name }}", size: {{ style.font.size }}), + font: {{ fontTypeName }}(name: "{{ style.font.name }}", size: {{ style.font.size }}) ?? {{ fontTypeName }}.systemFont(ofSize: {{ style.font.size }}), color: {{ colorTypeName }}( red: {{ style.color.color.red }}, green: {{ style.color.color.green }}, @@ -253,6 +253,13 @@ import AppKit self.init(string: string, attributes: style.attributes(includingParagraphStyle: includingParagraphStyle)) } } + +{{ accessModifier }} extension String{ + func style(as style: {{ styleTypeName }}) -> NSAttributedString { + return NSAttributedString(string: self, style: style) + } +} + {% else %} // No text style found {% endif %} From 0e8078781937785a195f5e53c2f7aa3505aeef32 Mon Sep 17 00:00:00 2001 From: Maxim Bunkov Date: Wed, 18 Nov 2020 11:43:48 +0500 Subject: [PATCH 2/2] fix(templates): fix my review remarks --- Demo/FugenDemo/Generated/ColorStyle.swift | 53 +++++++++++++++++++++-- Demo/FugenDemo/Generated/TextStyle.swift | 17 +++++--- Templates/ColorStyles.stencil | 23 +++++++--- Templates/TextStyles.stencil | 2 +- 4 files changed, 80 insertions(+), 15 deletions(-) diff --git a/Demo/FugenDemo/Generated/ColorStyle.swift b/Demo/FugenDemo/Generated/ColorStyle.swift index 00f7caf..4021b02 100644 --- a/Demo/FugenDemo/Generated/ColorStyle.swift +++ b/Demo/FugenDemo/Generated/ColorStyle.swift @@ -1,12 +1,57 @@ // swiftlint:disable all // Generated using Fugen - https://github.com/almazrafi/Fugen -#if canImport(UIKit) -import UIKit -#else -import AppKit +#if os(OSX) + import AppKit.NSColor + internal typealias ColorStyleExtension = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + internal typealias ColorStyleExtension = UIColor #endif +extension ColorStyleExtension{ + static let whisper = ColorStyleExtension( + red: 0.9137254953384399, + green: 0.9137254953384399, + blue: 0.9137254953384399, + alpha: 1.0) + static let snowDrift = ColorStyleExtension( + red: 0.8549019694328308, + green: 0.8549019694328308, + blue: 0.8509804010391235, + alpha: 1.0) + static let submarine = ColorStyleExtension( + red: 0.5803921818733215, + green: 0.5921568870544434, + blue: 0.5960784554481506, + alpha: 1.0) + static let eclipse = ColorStyleExtension( + red: 0.2235294133424759, + green: 0.2235294133424759, + blue: 0.2235294133424759, + alpha: 1.0) + static let lochinvar = ColorStyleExtension( + red: 0.25882354378700256, + green: 0.5882353186607361, + blue: 0.4901960790157318, + alpha: 1.0) + static let jellyBean = ColorStyleExtension( + red: 0.25882354378700256, + green: 0.4901960790157318, + blue: 0.5882353186607361, + alpha: 1.0) + static let daisyBush = ColorStyleExtension( + red: 0.35686275362968445, + green: 0.25882354378700256, + blue: 0.5882353186607361, + alpha: 0.75) + static let razzmatazz = ColorStyleExtension( + red: 0.8901960849761963, + green: 0.04313725605607033, + blue: 0.3607843220233917, + alpha: 1.0) +} + public struct ColorStyle: Equatable { // MARK: - Type Properties diff --git a/Demo/FugenDemo/Generated/TextStyle.swift b/Demo/FugenDemo/Generated/TextStyle.swift index e103602..4ea442b 100644 --- a/Demo/FugenDemo/Generated/TextStyle.swift +++ b/Demo/FugenDemo/Generated/TextStyle.swift @@ -35,7 +35,7 @@ public struct TextStyle: Equatable { /// Line height: 15.225 /// Letter spacing: 0.0 public static let caption = TextStyle( - font: UIFont(name: "SFProDisplay-Light", size: 13.0), + font: UIFont(name: "SFProDisplay-Light", size: 13.0) ?? UIFont.systemFont(ofSize: 13.0), color: UIColor( red: 0.2235294133424759, green: 0.2235294133424759, @@ -61,7 +61,7 @@ public struct TextStyle: Equatable { /// Line height: 16.0 /// Letter spacing: 0.125 public static let body = TextStyle( - font: UIFont(name: "SFProDisplay-Regular", size: 13.0), + font: UIFont(name: "SFProDisplay-Regular", size: 13.0) ?? UIFont.systemFont(ofSize: 13.0), color: UIColor( red: 0.2235294133424759, green: 0.2235294133424759, @@ -87,7 +87,7 @@ public struct TextStyle: Equatable { /// Line height: 17.575 /// Letter spacing: 0.2 public static let subtitle = TextStyle( - font: UIFont(name: "SFProDisplay-Regular", size: 15.0), + font: UIFont(name: "SFProDisplay-Regular", size: 15.0) ?? UIFont.systemFont(ofSize: 15.0), color: UIColor( red: 0.2235294133424759, green: 0.2235294133424759, @@ -113,7 +113,7 @@ public struct TextStyle: Equatable { /// Line height: 19.925 /// Letter spacing: 0.125 public static let title = TextStyle( - font: UIFont(name: "SFProDisplay-Medium", size: 17.0), + font: UIFont(name: "SFProDisplay-Medium", size: 17.0) ?? UIFont.systemFont(ofSize: 17.0), color: UIColor( red: 0.2235294133424759, green: 0.2235294133424759, @@ -139,7 +139,7 @@ public struct TextStyle: Equatable { /// Line height: 39.85 /// Letter spacing: 0.1 public static let largeTitle = TextStyle( - font: UIFont(name: "SFProDisplay-Bold", size: 34.0), + font: UIFont(name: "SFProDisplay-Bold", size: 34.0) ?? UIFont.systemFont(ofSize: 34.0), color: UIColor( red: 0.2235294133424759, green: 0.2235294133424759, @@ -496,3 +496,10 @@ public extension NSAttributedString { self.init(string: string, attributes: style.attributes(includingParagraphStyle: includingParagraphStyle)) } } + +public extension String{ + func styled(as style: TextStyle) -> NSAttributedString { + return NSAttributedString(string: self, style: style) + } +} + diff --git a/Templates/ColorStyles.stencil b/Templates/ColorStyles.stencil index 7d0391a..300d889 100644 --- a/Templates/ColorStyles.stencil +++ b/Templates/ColorStyles.stencil @@ -2,6 +2,7 @@ {% if colorStyles %} {% set accessModifier %}{% if options.publicAccess %}public{% else %}internal{% endif %}{% endset %} {% set styleTypeName %}{{ options.styleTypeName|default:"ColorStyle" }}{% endset %} +{% set colorTypeNameExtension %}{{ options.colorTypeNameExtension|default:"ColorStyleExtension" }}{% endset %} {% set colorTypeName %}{{ options.colorTypeName|default:"UIColor" }}{% endset %} {% macro propertyName name %}{{ name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords }}{% endmacro %} {% macro styleMutator propertyName propertyTypeName %} @@ -16,12 +17,24 @@ } {% endmacro %} -#if canImport(UIKit) -import UIKit -#else -import AppKit +#if os(OSX) + import AppKit.NSColor + internal typealias {{ colorTypeNameExtension }} = NSColor +#elseif os(iOS) || os(tvOS) || os(watchOS) + import UIKit.UIColor + internal typealias {{ colorTypeNameExtension }} = UIColor #endif +extension {{ colorTypeNameExtension }}{ + {% for style in colorStyles %} + static let {% call propertyName style.name %} = {{ colorTypeNameExtension }}( + red: {{ style.color.red }}, + green: {{ style.color.green }}, + blue: {{ style.color.blue }}, + alpha: {{ style.color.alpha }}) + {% endfor %} +} + {{ accessModifier }} struct {{ styleTypeName }}: Equatable { // MARK: - Type Properties @@ -35,7 +48,7 @@ import AppKit green: {{ style.color.green }}, blue: {{ style.color.blue }}, alpha: {{ style.color.alpha }} - ).color + ) {% endfor %} // MARK: - Instance Properties diff --git a/Templates/TextStyles.stencil b/Templates/TextStyles.stencil index ab9de95..8612124 100644 --- a/Templates/TextStyles.stencil +++ b/Templates/TextStyles.stencil @@ -255,7 +255,7 @@ import AppKit } {{ accessModifier }} extension String{ - func style(as style: {{ styleTypeName }}) -> NSAttributedString { + func styled(as style: {{ styleTypeName }}) -> NSAttributedString { return NSAttributedString(string: self, style: style) } }