开发者

How to change all my application texts (UILabel, UITableCell.....) color?

开发者 https://www.devze.com 2023-02-16 16:13 出处:网络
I would like to do something simi开发者_运维百科lar in CSS than \"BODY {color:red;}\" but in objectif C. I meanif I have 10 differents UIView, I would like to change all the UIView texts color in one

I would like to do something simi开发者_运维百科lar in CSS than "BODY {color:red;}" but in objectif C. I mean if I have 10 differents UIView, I would like to change all the UIView texts color in one time.

Cheers


Simple case - you somehow gathered them together

for (UIView *v in styledViews) {
    // apply current style here
}

I doubt that this is your case

Complex case - there are tons of styled views everywhere.

Disclaimer: I can't guarantee anything about following code, it works on my simulator, which doesn't mean it will not blow up in user's hands. I wrote it because it was fun and may help Thomas to solve his problem. I didn't check documentation thoroughly because it's already 5 a.m. here

1) Encapsulate style stuff in some StyleManager class (in this example applyCurrentStyle: will apply current style to any view passed to it). It should post notification each time style is changed (e.g. kStyleManagerNotificationStyleChanged)

2) Make UIView category (like UIView+Style) with public setStyleManager: method.

3) Implement it:

#import "UIView+Style.h"
#import <objc/runtime.h>

@interface StyleSubscription : NSObject {
    StyleManager *styleManager;
    NSObject *subscriber;
}
@property (readonly) StyleManager *styleManager;

- (id)initWithStyleManager:(NSObject*)p subscriber:(NSObject*)s;

@end


@implementation StyleSubscription

@synthesize styleManager;

- (id)initWithStyleManager:(StyleManager*)sManager subscriber:(NSObject*)s {
    if (self = [super init]) {
        styleManager = [sManager retain];
        subscriber = s;
    }

    return self;
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:subscriber 
                                                    name:kStyleManagerNotificationStyleChanged 
                                                  object:styleManager];
    [styleManager release];

    [super dealloc];
}


@end


@implementation UIView (Style)

static char styleSubsriptionKey;

- (StyleManager*)styleManager {
    StyleSubscription *s = objc_getAssociatedObject(self, &styleSubsriptionKey);
    return s.styleManager;
}


- (void)styleChanged:(NSNotification*)n {
    [[self styleManager] applyCurrentStyle:self];
}


- (void)setStyleManager:(StyleManager*)sManager {
    if ([self styleManager] == sManager) {
        return;
    }

    StyleSubscription *subscr = nil;

    if (sManager != nil) {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(styleChanged:) 
                                                     name:kStyleManagerNotificationStyleChanged 
                                                   object:sManager];
        subscr = [[[StyleSubscription alloc] initWithStyleManager:sManager 
                                                       subscriber:self] autorelease];
    }

    objc_setAssociatedObject(self, &styleSubsriptionKey, subscr, OBJC_ASSOCIATION_RETAIN);

    [sManager applyCurrentStyle:self];
}

@end

Each time style manager posts notification correspondent views will be updated with a new style. View will unsubscribe from style notifications automatically upon deallocation. Style manager can be removed explicitly [view setStyleManager:nil].


you can fix this in a nice way.here is a tutorial

http://dot-ios.blogspot.com/2013/02/design-uilabel-in-optimize-way-for.html

0

精彩评论

暂无评论...
验证码 换一张
取 消