change UILabel's text color for total project

2019-05-02 15:07发布

问题:

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 .

回答1:

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



回答2:

@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.



回答3:

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.