What is the recommended method of styling an iOS app? What is the recommended method of styling an iOS app? ios ios

What is the recommended method of styling an iOS app?


You could import a standard header file into all your controllers with several constants set for styling... example:

Styles.h

#define kFontSize 14#define kFontFamily @"Helevetica"

Controller

#import "Styles.h" // at the topmyLabel.font = [UIFont fontWithName:kFontFamily size:kFontSize];

I personally think Interface Builder is the best way to style, however this answers your question directly.


Update: I would recommend starting by understanding UIAppearance APIs, and seeing how well they suit your needs. UIAppearance is a convenient way to provide custom default stylization of specific controls' attributes at multiple levels (e.g. globally or contextually).


My original answer, which predated UIAppearance's availability:


since we're working with an object based language...

for the implementation, it depends on how you want it to behave/execute. when the implementation becomes nontrivial, i will often create a protocol. you could use class methods or instance methods and significantly optimize these types for your usage because you create fewer intermediate colors, fonts, images, etc.

a basic interface could take the form:

@protocol MONLabelThemeProtocol- (UIFont *)labelFont;- (UIColor *)labelTextColor;- (UITextAlignment)labelTextAlignment;// ...@end@protocol MONTableViewCellThemeProtocol- (UIFont *)tableViewCellFont;- (UIColor *)tableViewCellTextColor;- (UIImage *)tableViewCellImage;- (NSInteger)tableViewCellIndentationLevel;- (CGFloat)tableViewCellIndentationWidth;// ...@end

then a simple amalgamate theme could be declared like this:

@interface MONAmalgamateThemeBase : NSObject   < MONLabelThemeProtocol, MONTableViewCellThemeProtocol >{@protected    /* labels */    UIFont * labelFont;    UIColor * labelTextColor;    UITextAlignment labelTextAlignment;    // ...    /* table view cells */    UIFont * tableViewCellFont;    UIColor * tableViewCellTextColor;    UIImage * tableViewCellImage;    NSInteger tableViewCellIndentationLevel;    CGWidth tableViewCellIndentationWidth;    // ...}@end

in this example, the amalgamate defines the getters and dealloc and expects the subclasses to initialize the instance variables. you could also support lazy initialization if initialization times are high (e.g. uses many images).

then a specialization could take the form:

@interface MONDarkTheme : MONAmalgamateThemeBase@end@implementation MONDarkTheme- (id)init{    self = [super init];    if (nil != self) {        labelFont = [[UIFont boldSystemFontOfSize:15] retain];        labelTextColor = [[UIColor redColor] retain];        // and so on...    }    return self;}// ...@end/* declare another theme and set it up appropriately */@interface MONLightTheme : MONAmalgamateThemeBase@end

then just reuse the theme instances (e.g. MONDarkTheme) throughout the app to stylize the views. if you have a lot of themes or they are not trivial to construct, then you may want to create a collection for themes (theme manager). the amalgamate could also take a parameter, such as init with theme if your needs are simple. you can even configure objects to register for changes to themes, if you need support for dynamic changes.

finally, you can create a simple theme applier to make life easier - like so:

@interface UILabel (MONThemeAdditions)- (void)mon_applyMONLabelTheme:(id<MONLabelTheme>)theme;@end@implementation UILabel (MONThemeAdditions)- (void)mon_applyMONLabelTheme:(id<MONLabelTheme>)theme{    assert(theme);    if (nil == theme) return;    self.font = [theme labelFont];    self.textColor = [theme labelTextColor];    self.textAlignment = [theme labelTextAlignment];}@end


Frankly, the best way to go about this is to use Interface Builder. While it might seem nice to change a single constant somewhere in the code and have the entire app change styles, it never quite works out that way. Here are my reasonings:

1) Developers don't write interface code as well as interface builder does.Interface builder is a tool that has been refined, tested, and intreated over years. It offers fonts, text alignment, shadow, etc. It is backwards compatible for as far back as you'd ever want. It provides a very simple way for any number of developers and designers to jump in and work on something very straightforward.

2) There are always edge cases that you'll have to account for. Sure, a simple constant will do what you want most the time, but you'll eventually have to hack something in here and sneak something in there. The "simple" interface code you wrote to start off will grow and grow and grow. Other developers will have to maintain that code. You will have to maintain that code. You will have to file and fix bugs, tweak this, except that, etc. It will inevitably become a steaming pile of mess.

3) The more code you write, the more bugs you write. Interface builder is for building the 'look' of most iOS apps. Use it. Don't get too clever.

NOTE:I understand that Interface builder cannot do everything for all apps. There are cases that coding an interface is the only solution. This answer is simply a general "best practice" I use in the bulk of my apps.