Skip to content
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

Extensions for text and color styles template #75

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
53 changes: 49 additions & 4 deletions Demo/FugenDemo/Generated/ColorStyle.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 12 additions & 5 deletions Demo/FugenDemo/Generated/TextStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
}

21 changes: 17 additions & 4 deletions Templates/ColorStyles.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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 %}
}

Comment on lines +20 to +37
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we write another extension instead of this to the bottom of file?

{{ accessModifier }} extension {{ colorTypeName }} {

    // MARK: - Type Methods

    static func style(_ colorStyle: {{ styleTypeName }}) -> {{ colorTypeName }} {
        colorStyle.color
    }
}

Usage example:

view.backgroundColor = .style(.uiBackground)

{{ accessModifier }} struct {{ styleTypeName }}: Equatable {

// MARK: - Type Properties
Expand Down
9 changes: 8 additions & 1 deletion Templates/TextStyles.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, we should not use default system font, if font not found. You should validate all text styles on application start, for example:

do {
    try TextStyle.validate()
} catch {
    assertionFailure("\(error)")
}

Also, you can generate TextStyles using system font by specifying in templateOptions (will be available in next release):

textStyles:
  file:
    key: ...
    includedNodes:
      - ...
  destination: ...
  templateOptions:
    usingSystemFonts: true

color: {{ colorTypeName }}(
red: {{ style.color.color.red }},
green: {{ style.color.color.green }},
Expand Down Expand Up @@ -253,6 +253,13 @@ import AppKit
self.init(string: string, attributes: style.attributes(includingParagraphStyle: includingParagraphStyle))
}
}

{{ accessModifier }} extension String{
func styled(as style: {{ styleTypeName }}) -> NSAttributedString {
return NSAttributedString(string: self, style: style)
}
}
Comment on lines +257 to +261
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to align template code:

{{ accessModifier }} extension String {
    func styled(as style: {{ styleTypeName }}) -> NSAttributedString {
        NSAttributedString(string: self, style: style)
    }
}


{% else %}
// No text style found
{% endif %}