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!)試試這種方法。