You usually use UIAppearance methods to consistent styling througout your App, do you? Thats fine as long as you have only static styling. As soon as you need a dynamic styling behaviour you have to use other means to get the job done, as appearance is applied before your object gets added to the view hierarchy.
Styling is a sample implementation to keep all your font/color settings in one place. It is easy to understand and could easily be extended.
It provides methods to apply a style (UIButton/UILabel), generate an NSAttributedString or
modify an NSMutableAttributedString.
func apply (style: Style, forState state: UIControlState = UIControlState.Normal)func apply (style: Style)static func attributedString(with text: String, style: Style) -> NSAttributedStringfunc applyDecorated (style: StyleDecorator)There are three enum types you have to fill with your style needs.
- FontDesc
- SizeDesc
- ColorDesc
So if you need to style the string "The quick brown fox jumps over the lazy dog." where the "fox" and "dog" should have a different styling you create the style as follows:
let baseStyle = Style(fontDesc: fontDesc, sizeDesc: .Big, colorDesc: .Blue)
let foxStyle = StyleDecorator.Selection("fox", .NeueBold, .XXL, .Black)
let dogStyle = StyleDecorator.Selection("dog", .Oblique, .Small, .Red)
let decoratedStyle = DecoratedStyle(style: baseStyle, options: foxStyle, dogStyle)
let label.text = "The quick brown fox jumps over the lazy dog."
label.applyStyle(decoratedStyle)The Styles type provides some template methods for styles you use often.
yourLabel.applyStyle(Style.Headline)- If you provide more than one ColorDesc as option to a DecoratedStyle the last color option wins.
- The
static func font (for style: Style)always gives you a font back. If the font defined in your style can't be found, it falls back to a similar system font. - Styling of attributed text is limited to font an color.