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

Conversation

bimawa
Copy link

@bimawa bimawa commented Oct 11, 2020

Extended templates:

  • ColorStyles added UIColor return type. Now we can use style directly in colors
let color: UIColor = .primaryWhite40
  • TextStyles added default system font, if font not found.
  • TextStyles added String extension for make NSAttributesString directly:
let attributedText = "titleName".style(as: TextStyle.iosBodySmall.withColor(.primaryWhite))

@almazrafi please check before merge. I make changes in blind mode. No time for check in on my side. #74

@bimawa
Copy link
Author

bimawa commented Nov 14, 2020 via email

@almazrafi
Copy link
Owner

Better, if I update demo prj?

This will still break backward compatibility. It looks like we need to add a separate Colors type with static UIColor properties.

@bimawa
Copy link
Author

bimawa commented Nov 18, 2020

@almazrafi ok I did work by remarks, check it pls.

@bimawa
Copy link
Author

bimawa commented Nov 18, 2020

backward compatibility

Isn't Apple Way ^_^
I recommend prepare BRAKING CHANGES version for it.

@@ -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

Comment on lines +20 to +37
#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 %}
}

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)

Comment on lines +257 to +261
{{ accessModifier }} extension String{
func styled(as style: {{ styleTypeName }}) -> NSAttributedString {
return NSAttributedString(string: self, style: style)
}
}
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)
    }
}

@timbaev timbaev changed the title feat(teamplates): update templates Extensions for text and color styles template Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants