静态库/框架的iPhone设置样式

时间:2009-10-02 11:30:42

标签: iphone cocoa-touch

我正在寻找一种干净的方式来为应用程序,字体,颜色等定义全局样式。我已经构建了一个静态库,可以包含在其他应用程序中,每个应用程序都有自己的样式,边框,填充,背景。

我怎么能用cocoa启用一个系统,我可以为该库设置某些样式。 我看了Joe Hewitt的Three20库http://github.com/joehewitt/three20,(看看TTStyleSheet.h和实现),他做了一些我正在考虑的事情,但看起来有点过于复杂。

想知道是否有人有更好的系统..使用全球代表?

2 个答案:

答案 0 :(得分:1)

实际上,Three20对我来说非常好。您可以简单地继承TTStyle并覆盖其drawStyle(或类似的)方法。然后继承TTDefaultStyleSheet并使用[TTStyleSheet setGlobalStyleSheet:]

答案 1 :(得分:1)

实际上,我认为这是基于Three20的Style Sheet理念创建的这个类更容易实现这一点,而且没有开销。

<强> 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);

我想可以做出一些改进,以使这更好..如果你使用它并升级它可以联系我。

由于