iOS: Stylesheet/Theme for iOS app

走远了吗. 提交于 2019-12-24 11:42:52

问题


For one of my apps I currently allow for custom themes if the client makes a request (which they usually do, as it's a branded application). Sometimes this can be as simple as simply changing the colour scheme so I was wondering if there was a way to simplify the process. For example, if I had a class named ColourStyle and I specified its colour property values and then imported it to other classes, could I simply set it up so all I have to do is edit this one class and the others will naturally base their colours on whatever value is set in ColourStyle?

Would this be feasible, or is there a much easier way?


回答1:


One of the ways of doing this is that you adapt the new Appearance API. http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html

There's a good talk and slides about this if you log in to the apple's developer portal; Search for advanced ui customization.




回答2:


Style your apps using a syntax similar to CSS! https://github.com/tombenner/nui




回答3:


I created a Swift library that lets you do exactly this, plus it renders your styles live on the Storyboard canvas as a bonus!

https://github.com/daniel-hall/Stylish




回答4:


In iOS5+ there is the "appearance manager" which gives you global control over appearance qualities of various UI elements. Here's a tutorial: http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

If you need customization beyond what the appearance proxies can provide, you will have to create subclasses of things like UILabel and supply your own methods for customization. Unfortunately some things like UIButton can't be subclassed simply. You may have to roll your own.




回答5:


UIAppearance is one way to go, but we can achieve different properties of the styling engine (like earlier style application and style composition, etc.) by writing/using something new.

My approach allows to apply styled to your subclasses like so:

final class TitleLabel: UILabel, TitleFontStyle {}
final class BodyLabel: UILabel, BodyFontStyle, MultilineLabelStyle {}
final class CaptionLabel: UILabel, CaptionFontStyle, MultilineLabelStyle {}

where the style-marker protocols are defined 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 }
    ])
}

For complete usage example see the Example project.

For implementation details see Style.swift and RootStyle.swift.

Try this approach with the StyleSheet micro-framework (compatible with Carthage and CocoaPods!).



来源:https://stackoverflow.com/questions/13811422/ios-stylesheet-theme-for-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!