views:

233

answers:

2

I'm looking for a clean way to be able to define a global styles for an application, fonts , colors etc. I have build a static library to be included with other apps, each app has its own styles, border, fills, backgrounds.

How could i with cocoa enable a system that i could set certain styles for that library. I looked at Joe Hewitt's Three20 library http://github.com/joehewitt/three20, (look at TTStyleSheet.h and implementation) here he does something that i'm thinking about but it seems a little too complicated.

Wonder if anyone has a better system.. using a global delegate ?

+1  A: 

Three20 works pretty well for me actually. You could simply subclass TTStyle and override its drawStyle (or something similar) method. Then subclass TTDefaultStyleSheet and use [TTStyleSheet setGlobalStyleSheet:].

MrMage
+1  A: 

Actually it was easier that i thought this is the class i created to do this based off the Style sheets idea from Three20 with out the overheads.

GNStyle.h

 /*
 ----------------------------------------------------------------------------------------------------

 GNStyle.h

 ----------------------------------------------------------------------------------------------------

 Created by Shane Saunders on 02/10/2009.
 2009 GNative.
 www.gnative.com

 This is a condensed Style Sheet idea.
 Its only in Beta form and might need some work.
 Any upgrades please contact me with your changes
 shane/gnative/com

 Credit to Joe Hewitt for his idea from the Three20 library



 ----------------------------------------------------------------------------------------------------
*/


#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

/*-----------------------------------------------------------------------------------------------------------------------
    Style
-----------------------------------------------------------------------------------------------------------------------*/
#define GNSTYLE(_SELECTOR) [[GNStyle globalStyleSheet] styleWithSelector:@#_SELECTOR]
#define GNSTYLESTATE(_SELECTOR, _STATE) [[GNStyle globalStyleSheet] styleWithSelector:@#_SELECTOR forState:_STATE]
#define GNSTYLEVAR(_VARNAME) [GNSTYLESHEET _VARNAME]
#define GNSTYLESHEET ((id)[GNStyle globalStyleSheet])


@interface GNStyle : NSObject {
    NSMutableDictionary* _styles;
}

+ (GNStyle*)globalStyleSheet;
+ (void)setGlobalStyleSheet:(GNStyle*)styleSheet;

- (id)styleWithSelector:(NSString*)selector;
- (id)styleWithSelector:(NSString*)selector forState:(UIControlState)state;


@end


/*
 ----------------------------------------------------------------------------------------------------

 Default Style Sheet

 ----------------------------------------------------------------------------------------------------
 */

@interface GNDefaultStyle : GNStyle {

}

@end

GNStyle.m

/*
 ----------------------------------------------------------------------------------------------------

 GNStyle.m

 ----------------------------------------------------------------------------------------------------

 Created by Shane Saunders on 02/10/2009.
 2009 GNative.
 www.gnative.com

 This is a condensed Style Sheet idea.
 Its only in Beta form and might need some work.
 Any upgrades please contact me with your changes
 shane/gnative/com

 Credit to Joe Hewitt for his idea from the Three20 library



 ----------------------------------------------------------------------------------------------------
 */


#import "GNStyle.h"


static GNStyle* gStyleSheet = nil;



@implementation GNStyle


+ (GNStyle*)globalStyleSheet {
    if (!gStyleSheet) {
     gStyleSheet = [[GNDefaultStyle alloc] init];
    }
    return gStyleSheet;
}

+ (void)setGlobalStyleSheet:(GNStyle*)styleSheet {
    [gStyleSheet release];
    gStyleSheet = [styleSheet retain];
}


/* ---------------------------------------------------------------------------------------------------- */

- (id)init {
    if (self = [super init]) {
     _styles = nil;
    }
    return self;
}

- (void)dealloc { 
    [super dealloc];
}

- (void)didReceiveMemoryWarning:(void*)object {

}

/* ---------------------------------------------------------------------------------------------------- */


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

- (id)styleWithSelector:(NSString*)selector forState:(UIControlState)state {
    NSString* key = state == UIControlStateNormal ? selector : [NSString stringWithFormat:@"%@%d", selector, state];
    GNStyle* style = [_styles objectForKey:key];

    if (!style) {
     SEL sel = NSSelectorFromString(selector);
     if ([self respondsToSelector:sel]) {
      style = [self performSelector:sel withObject:(id)state];
      if (style) {
       if (!_styles) {
        _styles = [[NSMutableDictionary alloc] init];
       }
       [_styles setObject:style forKey:key];
      }
     }
    }
    return style;
}

@end


/*
 ----------------------------------------------------------------------------------------------------

 Default Style Sheet

 ----------------------------------------------------------------------------------------------------
*/


@implementation GNDefaultStyle


-(UIColor*)colorOne
{
    return [UIColor redColor];
}


-(UIColor*)stateColor:(UIControlState)state
{   
    if (state == UIControlStateHighlighted)
     return [UIColor yellowColor];
    else
     return [UIColor greenColor];

}

@end

The usage is very easy..

#import GNStyle.h


UIColor *colorOne = GNSTYLE(colorOne);  
UIColor *normalColor = GNSTYLESTATE(stateColor:, UIControlStateNormal);
UIColor *highlightColor = GNSTYLESTATE(stateColor:, UIControlStateHighlighted);

I guess there ar esome changes that could be made to make this better.. if you do use this and upgrade it could you contact me.

Thanks

Gnative