Skip to content

Commit

Permalink
* Add new data source constructors for convenience
Browse files Browse the repository at this point in the history
* Rename "class name" to "selector" as it pertains to styles
  • Loading branch information
joehewitt committed Apr 9, 2009
1 parent 6791a11 commit 248105a
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 35 deletions.
8 changes: 4 additions & 4 deletions samples/TTCatalog/Classes/ButtonTestController.m
Expand Up @@ -30,8 +30,8 @@ - (TTStyle*)embossedButton:(UIControlState)state {
[TTLinearGradientFillStyle styleWithColor1:RGBCOLOR(255, 255, 255)
color2:RGBCOLOR(216, 221, 231) next:
[TTSolidBorderStyle styleWithColor:RGBCOLOR(161, 167, 178) width:1 next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(0, 0, -1, 0) next:
[TTTextStyle styleWithFont:[UIFont boldSystemFontOfSize:13]
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(4, 8, 4-1, 8) next:
[TTTextStyle styleWithFont:[UIFont boldSystemFontOfSize:14]
color:TTSTYLEVAR(linkTextColor)
shadowColor:[UIColor colorWithWhite:255 alpha:0.4]
shadowOffset:CGSizeMake(0, -1) next:nil]]]]]]];
Expand All @@ -43,8 +43,8 @@ - (TTStyle*)embossedButton:(UIControlState)state {
[TTLinearGradientFillStyle styleWithColor1:RGBCOLOR(225, 225, 225)
color2:RGBCOLOR(196, 201, 221) next:
[TTSolidBorderStyle styleWithColor:RGBCOLOR(161, 167, 178) width:1 next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(0, 0, -1, 0) next:
[TTTextStyle styleWithFont:[UIFont boldSystemFontOfSize:13]
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(4, 8, 4-1, 8) next:
[TTTextStyle styleWithFont:[UIFont boldSystemFontOfSize:14]
color:[UIColor whiteColor] shadowColor:[UIColor colorWithWhite:255 alpha:0.4]
shadowOffset:CGSizeMake(0, -1) next:nil]]]]]]];
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/TTButton.m
Expand Up @@ -138,10 +138,10 @@ @implementation TTButton
///////////////////////////////////////////////////////////////////////////////////////////////////
// class public

+ (TTButton*)buttonWithStyle:(NSString*)className title:(NSString*)title {
+ (TTButton*)buttonWithStyle:(NSString*)selector title:(NSString*)title {
TTButton* button = [[[TTButton alloc] initWithFrame:CGRectZero] autorelease];
[button setTitle:title forState:UIControlStateNormal];
[button setStylesWithClassName:className];
[button setStylesWithSelector:selector];

return button;
}
Expand Down Expand Up @@ -403,19 +403,19 @@ - (void)setStyle:(TTStyle*)style forState:(UIControlState)state {
content.style = style;
}

- (void)setStylesWithClassName:(NSString*)className {
- (void)setStylesWithSelector:(NSString*)selector {
TTStyleSheet* ss = [TTStyleSheet globalStyleSheet];

TTStyle* normalStyle = [ss styleWithClassName:className forState:UIControlStateNormal];
TTStyle* normalStyle = [ss styleWithSelector:selector forState:UIControlStateNormal];
[self setStyle:normalStyle forState:UIControlStateNormal];

TTStyle* highlightedStyle = [ss styleWithClassName:className forState:UIControlStateHighlighted];
TTStyle* highlightedStyle = [ss styleWithSelector:selector forState:UIControlStateHighlighted];
[self setStyle:highlightedStyle forState:UIControlStateHighlighted];

TTStyle* selectedStyle = [ss styleWithClassName:className forState:UIControlStateSelected];
TTStyle* selectedStyle = [ss styleWithSelector:selector forState:UIControlStateSelected];
[self setStyle:selectedStyle forState:UIControlStateSelected];

TTStyle* disabledStyle = [ss styleWithClassName:className forState:UIControlStateDisabled];
TTStyle* disabledStyle = [ss styleWithSelector:selector forState:UIControlStateDisabled];
[self setStyle:disabledStyle forState:UIControlStateDisabled];
}

Expand Down
1 change: 0 additions & 1 deletion src/TTScrollView.m
Expand Up @@ -496,7 +496,6 @@ - (UIEdgeInsets)squareTouchEdges:(UIEdgeInsets)edges {
CGFloat midY = edges.top + (height/2);

return UIEdgeInsetsMake(midY - d/2, midX - d/2, midY + d/2, midX + d/2);
return UIEdgeInsetsMake(edges.top, edges.left, edges.top + d, edges.left + d);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/TTStyleSheet.m
Expand Up @@ -45,19 +45,19 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// public

- (TTStyle*)styleWithClassName:(NSString*)className {
return [self styleWithClassName:className forState:UIControlStateNormal];
- (TTStyle*)styleWithSelector:(NSString*)selector {
return [self styleWithSelector:selector forState:UIControlStateNormal];
}

- (TTStyle*)styleWithClassName:(NSString*)className forState:(UIControlState)state {
- (TTStyle*)styleWithSelector:(NSString*)selector forState:(UIControlState)state {
NSString* key = state == UIControlStateNormal
? className
: [NSString stringWithFormat:@"%@%d", className, state];
? selector
: [NSString stringWithFormat:@"%@%d", selector, state];
TTStyle* style = [_styles objectForKey:key];
if (!style) {
SEL selector = NSSelectorFromString(className);
if ([self respondsToSelector:selector]) {
style = [self performSelector:selector withObject:(id)state];
SEL sel = NSSelectorFromString(selector);
if ([self respondsToSelector:sel]) {
style = [self performSelector:sel withObject:(id)state];
if (style) {
if (!_styles) {
_styles = [[NSMutableDictionary alloc] init];
Expand Down
2 changes: 1 addition & 1 deletion src/TTStyledText.m
Expand Up @@ -105,7 +105,7 @@ - (void)layoutFrames {
style = linkStyle;
}
TTStyledSpanNode* span = (TTStyledSpanNode*)node;
TTStyle* spanStyle = [[TTStyleSheet globalStyleSheet] styleWithClassName:span.className];
TTStyle* spanStyle = [[TTStyleSheet globalStyleSheet] styleWithSelector:span.className];
if (spanStyle) {
style = spanStyle;
}
Expand Down
30 changes: 26 additions & 4 deletions src/TTTableViewDataSource.m
Expand Up @@ -209,10 +209,6 @@ @implementation TTListDataSource
///////////////////////////////////////////////////////////////////////////////////////////////////
// class public

+ (TTListDataSource*)dataSourceWithObjectsArray:(NSMutableArray*)items {
return [[[self alloc] initWithItems:items] autorelease];
}

+ (TTListDataSource*)dataSourceWithObjects:(id)object,... {
NSMutableArray* items = [NSMutableArray array];
va_list ap;
Expand All @@ -226,6 +222,10 @@ + (TTListDataSource*)dataSourceWithObjects:(id)object,... {
return [[[self alloc] initWithItems:items] autorelease];
}

+ (TTListDataSource*)dataSourceWithItems:(NSMutableArray*)items {
return [[[self alloc] initWithItems:items] autorelease];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

Expand Down Expand Up @@ -314,6 +314,28 @@ + (TTSectionedDataSource*)dataSourceWithObjects:(id)object,... {
return [[[self alloc] initWithItems:items sections:sections] autorelease];
}

+ (TTSectionedDataSource*)dataSourceWithArrays:(id)object,... {
NSMutableArray* items = [NSMutableArray array];
NSMutableArray* sections = [NSMutableArray array];
va_list ap;
va_start(ap, object);
while (object) {
if ([object isKindOfClass:[NSString class]]) {
[sections addObject:object];
} else {
[items addObject:object];
}
object = va_arg(ap, id);
}
va_end(ap);

return [[[self alloc] initWithItems:items sections:sections] autorelease];
}

+ (TTSectionedDataSource*)dataSourceWithItems:(NSArray*)items sections:(NSArray*)sections {
return [[[self alloc] initWithItems:items sections:sections] autorelease];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

Expand Down
2 changes: 1 addition & 1 deletion src/TTThumbView.m
Expand Up @@ -10,7 +10,7 @@ - (id)initWithFrame:(CGRect)frame {
self.opaque = YES;
self.clipsToBounds = YES;

[self setStylesWithClassName:@"thumbView:"];
[self setStylesWithSelector:@"thumbView:"];
}
return self;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Three20/TTButton.h
Expand Up @@ -7,7 +7,7 @@

@property(nonatomic,retain) UIFont* font;

+ (TTButton*)buttonWithStyle:(NSString*)className title:(NSString*)title;
+ (TTButton*)buttonWithStyle:(NSString*)selector title:(NSString*)title;

- (NSString*)titleForState:(UIControlState)state;
- (void)setTitle:(NSString*)title forState:(UIControlState)state;
Expand All @@ -17,7 +17,14 @@

- (TTStyle*)styleForState:(UIControlState)state;
- (void)setStyle:(TTStyle*)style forState:(UIControlState)state;
- (void)setStylesWithClassName:(NSString*)className;

/**
* Sets the styles for all control states using a single style selector.
*
* The method for the selector must accept a single argument for the control state. It will
* be called to return a style for each of the different control states.
*/
- (void)setStylesWithSelector:(NSString*)selector;

- (void)suspendLoadingImages:(BOOL)suspended;

Expand Down
6 changes: 3 additions & 3 deletions src/Three20/TTGlobal.h
Expand Up @@ -67,10 +67,10 @@
///////////////////////////////////////////////////////////////////////////////////////////////////
// Style helpers

#define TTSTYLE(_CLASSNAME) [[TTStyleSheet globalStyleSheet] styleWithClassName:@#_CLASSNAME]
#define TTSTYLE(_SELECTOR) [[TTStyleSheet globalStyleSheet] styleWithSelector:@#_SELECTOR]

#define TTSTYLESTATE(_CLASSNAME, _STATE) [[TTStyleSheet globalStyleSheet] \
styleWithClassName:@#_CLASSNAME forState:_STATE]
#define TTSTYLESTATE(_SELECTOR, _STATE) [[TTStyleSheet globalStyleSheet] \
styleWithSelector:@#_SELECTOR forState:_STATE]

#define TTSTYLEVAR(_VARNAME) [(id)[TTStyleSheet globalStyleSheet] _VARNAME]

Expand Down
4 changes: 2 additions & 2 deletions src/Three20/TTStyleSheet.h
Expand Up @@ -9,8 +9,8 @@
+ (TTStyleSheet*)globalStyleSheet;
+ (void)setGlobalStyleSheet:(TTStyleSheet*)styleSheet;

- (TTStyle*)styleWithClassName:(NSString*)className;
- (TTStyle*)styleWithClassName:(NSString*)className forState:(UIControlState)state;
- (TTStyle*)styleWithSelector:(NSString*)selector;
- (TTStyle*)styleWithSelector:(NSString*)selector forState:(UIControlState)state;

- (void)freeMemory;

Expand Down
17 changes: 17 additions & 0 deletions src/Three20/TTTableViewDataSource.h
Expand Up @@ -62,6 +62,7 @@
@property(nonatomic,readonly) NSMutableArray* items;

+ (TTListDataSource*)dataSourceWithObjects:(id)object,...;
+ (TTListDataSource*)dataSourceWithItems:(NSMutableArray*)items;

- (id)initWithItems:(NSArray*)items;

Expand All @@ -74,8 +75,24 @@
NSMutableArray* _items;
}

/**
* Objects should be in this format:
*
* @"section title", item, item, @"section title", item, item, ...
*
*/
+ (TTSectionedDataSource*)dataSourceWithObjects:(id)object,...;

/**
* Objects should be in this format:
*
* @"section title", arrayOfItems, @"section title", arrayOfItems, ...
*
*/
+ (TTSectionedDataSource*)dataSourceWithArrays:(id)object,...;

+ (TTSectionedDataSource*)dataSourceWithItems:(NSArray*)items sections:(NSArray*)sections;

- (id)initWithItems:(NSArray*)items sections:(NSArray*)sections;

- (NSArray*)lettersForSectionsWithSearch:(BOOL)withSearch withCount:(BOOL)withCount;
Expand Down
4 changes: 2 additions & 2 deletions src/UIViewAdditions.m
Expand Up @@ -190,12 +190,12 @@ - (CGPoint)offsetFromView:(UIView*)otherView {

- (CGFloat)orientationWidth {
return UIDeviceOrientationIsLandscape(TTDeviceOrientation())
? self.height: self.width;
? self.height : self.width;
}

- (CGFloat)orientationHeight {
return UIDeviceOrientationIsLandscape(TTDeviceOrientation())
? self.width: self.height;
? self.width : self.height;
}

- (UIScrollView*)findFirstScrollView {
Expand Down

0 comments on commit 248105a

Please sign in to comment.