TTDefaultStyleSheet, TTStyle usage?

49 views
Skip to first unread message

Eric Higginbotham

unread,
Jun 11, 2009, 10:29:42 AM6/11/09
to Three20
Good morning Three20 people.

I have been very pleased with how fast Three20 has let me prototype my
application. Unfortunately, I am now wanting to refine the look and
feel beyond the default rendering of the controls and I have hit a
small hurdle. How do I do that?

I have tried searching for the terms in the subject above as well as
browsing the code for TTSample and Three20 itself and even paging
through the 207 discussions that are in this group. I am sure this is
probably something simple, but I haven't yet found it (most of my
looking was done in the wee hours of the morning, but...). Any help
would be greatly appreciated.

I feel compelled to add the following very sincere bit:
Joe, Thank you for creating and releasing Three20.

- Eric

klazuka

unread,
Jun 11, 2009, 11:14:29 AM6/11/09
to Three20
I've only used it a little bit, so take this for what it's worth.

Basically, you can just subclass TTDefaultStyleSheet, override the
appropriate methods, and register your subclass as the global
stylesheet:
[TTStyleSheet setGlobalStyleSheet:[[[MyStyleSheet alloc] init]
autorelease]];

I'm sure there is more advanced usage, but for the basic stuff, this
will get you going.

-keith

klazuka

unread,
Jun 11, 2009, 12:03:17 PM6/11/09
to Three20
I'm digging into TTStyleSheet right now. If you want to see how client
code would use TTStyleSheet, take a look at TTButton.m.

Basically, TTStyleSheet has 3 responsibilities:
1) manage access to the global stylesheet instance
2) provide a cached lookup system for TTStyles
3) provide a dynamic lookup system so that methods on your custom
subclass can be called, even though they may not be defined in the
baseclass.

Responsibility 2 is implemented by these methods:
- (TTStyle*)styleWithSelector:(NSString*)selector;
- (TTStyle*)styleWithSelector:(NSString*)selector forState:
(UIControlState)state;

If you look closely at styleWithSelector:forState:, you'll see that it
just uses a mutable dictionary to memoize the TTStyle value for a
given selector/state pair (the UIControlState is optional). If the
value is not found in the cache, the stylesheet will be sent
'selector' as a message to construct the value. The value will then be
stored in the cache so that future lookups don't require the TTStyle
to be constructed again, nor memory wasted on copies of an immutable
value.

As for responsibility #3, TTStyleSheet allows you to use a string to
specify which style to lookup. This gives you the freedom to decide at
runtime which style to use while also preventing compile-time warnings/
errors that TTStyleSheet does not implement a method (e.g. your code
tries to call method "myCrazyButtonStyle:", which is defined in
MyCustomStyleSheet, but is not part of TTStyleSheet's interface).

As I mentioned above, take a look at TTButton.m

-keith

Eric Higginbotham

unread,
Jun 11, 2009, 12:31:11 PM6/11/09
to Three20
Keith,

Thank you for the direction. I will be working on this later today
and expect it to go much smoother now.

- Eric

Eric Higginbotham

unread,
Jun 11, 2009, 12:43:23 PM6/11/09
to Three20
I couldn't help myself so I went ahead and tried it as your first post
suggested (going to spend more time reviewing TTStyleSheet later) and
it worked great.

Thank you!

- Eric

p.s. I decided to crank the red value up while leaving the other
values the same to see if it worked. RGBCOLOR(255, 140, 168) is a
very nice pinkish color.

klazuka

unread,
Jun 11, 2009, 2:55:12 PM6/11/09
to Three20
I wish I could edit my prior post. Oh well.

I was mistaken about TTStyles being immutable value objects. They are
not: you can append styles to the style linked list at any time. That
being said, when reading my previous post, please mentally replace
"The value will then be stored in the cache so that future lookups
don't require the TTStyle to be constructed again, nor memory wasted
on copies of an immutable value."
WITH
"The value will then be stored in the cache so that future lookups
don't require the TTStyle to be constructed again, and that TTStyle
instance will be shared among all clients."

-keith
Reply all
Reply to author
Forward
0 new messages