2009-10-02 6 views
2

Je cherche une manière propre de pouvoir définir des styles globaux pour une application, des polices, des couleurs etc. J'ai construit une bibliothèque statique pour être incluse avec d'autres applications, chacune application a ses propres styles, bordure, remplissages, arrière-plans.Styles de paramètres iPhone pour bibliothèque statique/cadre

Comment pourrais-je avec le cacao activer un système que je pourrais définir certains styles pour cette bibliothèque. J'ai regardé la bibliothèque Three20 de Joe Hewitt http://github.com/joehewitt/three20, (regardez TTStyleSheet.h et la mise en œuvre) ici, il fait quelque chose que je pense mais il semble un peu trop compliqué.

Je me demande si quelqu'un a un meilleur système .. en utilisant un délégué global?

Répondre

1

Three20 fonctionne plutôt bien pour moi en fait. Vous pouvez simplement sous-classer TTStyle et remplacer sa méthode drawStyle (ou quelque chose de similaire). Puis sous-classe TTDefaultStyleSheet et utiliser [TTStyleSheet setGlobalStyleSheet:].

1

En fait, il était plus facile que je pensais que c'est la classe que j'ai créée pour le faire sur la base de l'idée de feuilles de style de Three20 sans les frais généraux.

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 

L'utilisation est très facile ..

#import GNStyle.h 


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

Je suppose qu'il ar esome des changements qui pourraient être fait pour le rendre meilleur .. si vous l'utilisez et mettez à niveau, vous pouvez me contacter.

Remerciements

Questions connexes