2009-10-02 42 views
2

我正在尋找一種乾淨的方式,以便能夠爲應用程序,字體,顏色等定義全局樣式。我已經構建了一個靜態庫以包含在其他應用程序中,每個應用程序有它自己的樣式,邊框,填充,背景。靜態庫/框架的iPhone設置樣式

我怎麼可能與可可啓用一個系統,我可以爲該庫設置某些樣式。 我看着Joe Hewitt的Three20庫http://github.com/joehewitt/three20,(看看TTStyleSheet.h和實現),他在這裏做了一些我在想的事情,但看起來有些複雜。

不知道有人有更好的系統..使用全球代表?

回答

1

Three20對我來說確實很好。你可以簡單地子類TTStyle並覆蓋其drawStyle(或類似的)方法。然後子類TTDefaultStyleSheet和使用[TTStyleSheet setGlobalStyleSheet:]

1

事實上,我認爲這是我創建的課程,以此爲基礎,根據Three20中的樣式表構思制定出的開銷。

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 

用法很容易..

#import GNStyle.h 


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

我想有AR esome變化,這些變化使之更好..如果你使用這個並升級它,你可以聯繫我。

謝謝