UIAppearance
是一条路可走,但我们可以通过写/使用一些新的样式引擎(如早期风格的应用程序和样式成分等)的不同性质。
我的方法,用户可以应用样式到您的子类like so:
final class TitleLabel: UILabel, TitleFontStyle {}
final class BodyLabel: UILabel, BodyFontStyle, MultilineLabelStyle {}
final class CaptionLabel: UILabel, CaptionFontStyle, MultilineLabelStyle {}
这里的款式,标志协议定义like so:
protocol TitleFontStyle {}
protocol BodyFontStyle {}
protocol CaptionFontStyle {}
protocol MultilineLabelStyle {}
func appStyle(palette: PaletteProtocol) -> StyleProtocol {
return StyleSheet(styles: [
Style<TitleFontStyle, UILabel> { $0.font = palette.fonts.titleFont },
Style<BodyFontStyle, UILabel> { $0.font = palette.fonts.bodyFont },
Style<CaptionFontStyle, UILabel> { $0.font = palette.fonts.captionFont },
Style<MultilineLabelStyle, UILabel> { $0.numberOfLines = 0 }
])
}
有关完整的使用示例见Example项目。
实施细则见Style.swift
和RootStyle.swift
。
用StyleSheet微框架(兼容Carthage和CocoaPods!)试试这种方法。