Three20 Stylesheets iPhone Tutorial
*Note* SOME of this tutorial is now deprecated. I will be updating this tutorial soon, but until then please refer to my new TableItem tutorial for Step 4 of this tutorial.
Introduction to Three20 Stylesheets
In this tutorial, I am going to show you how to use CSS like stylesheets in your iPhone Apps.
If (like me) you’re coming to the iPhone platform from a Web Design perspective, you should be very familiar with CSS and external stylesheets. Unfortunately right now, there is no built-in equivalent to CSS for building iPhone apps.
Lucky for us Joe Hewitt has created a way with his Three20 iPhone Library. With Three20, you can easily change something like this:
To something like this:
Using this stylesheet:
Although I am going to show you some cool features of Three20, I’m NOT going to show you how to install Three20 in your Apps, nor build a complete App with Three20. Nope, today I’m only going to show you how to use Three20’s stylesheet capabilities.
I’m assuming a basic knowledge of Three20 and an intermediate knowledge of Cocoa and iPhone Dev. I’m also assuming you already have a UILabel or something to that you want to change the font of.
If you want to get your hands dirty, there is a sample app using this same code on my GitHub Three20 tutorial repository.
Let’s start the show
First things first, create your stylesheet.
-
Create a new blank file. Do this by going File >> New File, then clicking NSObject Subclass. Name the file “BNDefaultStylesheet” (i like sticking to Three20’s naming conventions by replacing the abbreviation TT with the abbreviation BN for “Basic Navigation”).
Copy the following code into BNDefaultStylesheet.h:
#import "Three20/TTStyleSheet.h" #import "Three20/TTDefaultStyleSheet.h" @interface BNDefaultStyleSheet : TTDefaultStyleSheet @end
Then copy the following into BNDefaultStylesheet.m:
#import "BNDefaultStyleSheet.h" #import "Three20/TTStyle.h" #import "Three20/TTShape.h" #import "Three20/TTURLCache.h" /////////////////////////////////////////////////////////////////////////////////////////////////// @implementation BNDefaultStyleSheet @end
- Register the new stylesheet into the app delegate.
Do this by making BasicNavigationAppDelegate.m look like this:
#import "BasicNavigationAppAppDelegate.h" #import "RootViewController.h" #import "Three20/Three20.h" #import "BNDefaultStyleSheet.h" @implementation BasicNavigationAppAppDelegate @synthesize window; @synthesize navigationController; #pragma mark - #pragma mark Application lifecycle - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after app launch [TTStyleSheet setGlobalStyleSheet:[[[BNDefaultStyleSheet alloc] init] autorelease]]; [window addSubview:[navigationController view]]; [window makeKeyAndVisible]; } - (void)applicationWillTerminate:(UIApplication *)application { // Save data if appropriate } #pragma mark - #pragma mark Memory management - (void)dealloc { [navigationController release]; [window release]; [super dealloc]; } @end - Make your first font style.
Add a new font style and color by making BNDefaultStylesheet.h look like this:
#import "Three20/TTStyleSheet.h" #import "Three20/TTDefaultStyleSheet.h" @interface BNDefaultStyleSheet : TTDefaultStyleSheet @property(nonatomic,readonly) UIColor* myFirstColor; @property(nonatomic,readonly) UIFont* myFirstFont; @end
And making BNDefaultStylesheet.m look like this:
#import "BNDefaultStyleSheet.h" #import "Three20/TTStyle.h" #import "Three20/TTShape.h" #import "Three20/TTURLCache.h" /////////////////////////////////////////////////////////////////////////////////////////////////// @implementation BNDefaultStyleSheet /////////////////////////////////////////////////////////////////////////////////////////////////// // styles /////////////////////////////////////////////////////////////////////////////////////////////////// // public colors - (UIColor*)myFirstColor { return RGBCOLOR(80, 110, 140); } - (UIColor*)mySecondColor { return [UIColor grayColor]; } /////////////////////////////////////////////////////////////////////////////////////////////////// // public fonts - (UIFont*)myFirstFont { return [UIFont boldSystemFontOfSize:15]; } - (UIFont*)mySecondFont { return [UIFont systemFontOfSize:14]; } @end - Use your new style.
Change the font to point to the font style by making BNCell.m look like this:
#import "BNCell.h" #import "BNDefaultStylesheet.h" /////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////// ////// BNSubtextTableFieldCell //// ///////////////////////////////////////// @implementation BNSubtextTableFieldCell /////////////////////////////////////////////////////////////////////////////////////////////////// - (void)setObject:(id)object { if (_field != object) { [super setObject:object]; TTSubtextTableField* field = object; _label.text = field.text; _label.font = TTSTYLEVAR(myFirstFont); _label.textColor = TTSTYLEVAR(myFirstColor); _label.adjustsFontSizeToFitWidth = YES; _subtextLabel.text = field.subtext; } } @end ///////////////////////////////////////////////////////////////////////////////////////////////////
Voila! You should now have your first App using Three20 Stylesheets. Your App should now have blue labels instead of black ones like so:
I’ll be refining this tutorial in the future, but if you find any glaring omissions please point em out.
AND To people following my tutorials: … DON’T BE SO PROUD. Ask questions if you have em, I’m cool with helping you get this stuff working in your Apps.
Comments on this Post
Questions? Comments? Kindly-worded insults?
Thats right, it's all welcome folks! Step right up and leave a your mark for the ages!



September 11, 2009 - Mark Thistle
Hi Matt,
Loving the tutorials. Joe’s code and your tutorials have saved me a ton of development work and let me concentrate on other areas of my app. I was working on an auto-expanding table cell view when I found Joe’s code and your tutorial.
Keep up the great work and I will keep you posted when I complete the app.
September 11, 2009 - MattV
Hey thanks a lot, glad you are enjoying them and finding them useful! Got any ideas for a good topic for my next tutorial?
Cheers,
-MattV
September 16, 2009 - MichaelG
Thanks…this is good stuff. Have you been applied these styles to the TTLauncherView to change the text size and font. If so, can you give me a push in right direction?
Thanks,
September 17, 2009 - MattV
Michael,
To be honest I haven’t played around with TTLauncherView yet but I imagine that changing the font would require subclassing TTLauncherView, but I could be wrong.
Good luck, and report back how it went!
-MattV
September 20, 2009 - Mike
The only issue I see with this is it litters your code with warnings for all the TTSTYLEVAR calls. Do you have any clean solution to this?
January 12, 2010 - Amit
Hi Matt,
I created a simple app similar TTCatalog->StyledTextTestController. On clicking a hyperlink, I dont see the TTWebview’s being loaded (like its done in TTCatalog). Any ideas would be appreciated?
Thanks
Amit
February 8, 2010 - Raghav
Hi, Could you pls post some tutorial on how to use the PhotoViewer API
February 14, 2010 - fabian
Hi Matt,
i was wondering if i can use a .xib-File to design a custom cell (e.g. with image, title, subtitle)?
April 7, 2010 - wayne
Hey
i’m using 320 in monotouch – i got the bindings working ok and with a few tweaks got the style stuff going.
I want to style the tabbar and/or the tabs themselves – how do i a: do this and b: find out what the properties are that I need to override so that i can do this quicker in future?
cheers
w://
April 7, 2010 - wayne
you can ignore that last post – i found TabBarTintColor – is that all there is that can be styled?
w://
April 21, 2010 - Joe
Yay! This is terrific – thank you so much for sharing and for offering to field questions.
I wanted to set a global status bar and nav bar tint color (vs. the defaults that TTViewController uses), so I added a Category on TTViewController and added a method, conformToCommonStyle … which I then call from each of my VCs. But a) the method name sounds clunky in terms of Cocoa Design Patterns, and b) “there’s got to be a better way!” I’m wondering if a stylesheet is just what the doctor ordered.
April 21, 2010 - Joe
Sweet! I can override navigationBarTintColor easily enough … but I don’t see a way to do that for the status bar style in one fell swoop.
Unless … I could alter TTViewController over in Three20 and have it grab [[UIApplication sharedApplication] statusBarStyle], which I could set in the app delegate. I’d rather not change my copy of the Three20 source though … which brings us back to Categories.
Thoughts?
April 22, 2010 - MattV
Joe,
Im actually not sure if there is a way to change the status bar colour from three20, your on your own for that one, sorry.
Good luck!
-Matt
April 26, 2010 - Joe
Matt,
No worries. I dug a bit more and, yep, you’re right – there’s no app-wide way to do it out-of-the-box. That’s OK. That’s why we have Plan B.
July 10, 2010 - How To Program A Fully Functional iOS Photo Viewer Quickly With Three20 | iPhone iOS 4 iPad SDK Development Tutorials, Programming Tips, News
[...] You can also find some more guides on using Three20 here: Getting The Most Out Of The Three20 iOS Library [...]