change UILabel's text color for total project

2019-05-02 15:12发布

i want to change all the UILabel's text color for total app how can i do this .is there any simple way to change label color.

i tried this ,but i want to change write this code in every class

for( UIView *view in [testScroll subviews])
    {
        if([view isKindOfClass:[UILabel class]])
        {
            [(UILabel *)view setTextColor:[UIColor whiteColor]];

        }
        else if([view isKindOfClass:[UIView class]])

    }

Do anyone have simple method .

3条回答
祖国的老花朵
2楼-- · 2019-05-02 15:34

Well if you're targeting iOS 5+ you could do that really easy (inside you appdelegate):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //...

    //This color will affect every label in your app
    [[UILabel appearance] setTextColor:[UIColor redColor]]; 

    //...

    return YES;
}

This works because UILabel conforms to UIAppearance protocol, so you could customize any class that conforms to the protocol like this. For more information on this here is a nice tutorial to get you started and here is Apple's docs on the protocol

查看更多
一纸荒年 Trace。
3楼-- · 2019-05-02 15:34

Create a subclass of the UILabel. You can override the init functions where you can set the text Color you want. Use that subclass throughout your project.

查看更多
Bombasti
4楼-- · 2019-05-02 15:36
@interface CustomLabel : UILabel
 @end

 @implementation CustomLabel

 - (id)init{
  if ([super init]){
      self.textColor = // Putt here the color you want.
  }

   return self;
 }

and now jus use your CustomLabel.

查看更多
登录 后发表回答