5

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 .

user2197875
  • 171
  • 3
  • 12

3 Answers3

12

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

Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • 1
    Blue is prettier: `[[UILabel appearance] setColor:[UIColor blueColor]];` ^^ – Groot Jun 05 '13 at 10:22
  • Changed my mind: `[[UILabel appearance] setColor:[UIColor purpleColor]];` – Groot Jun 05 '13 at 10:23
  • +1 simple and affective. Not sure why you would do it any other way. – Popeye Jun 05 '13 at 10:23
  • It seems there could be some issues using this method according to discussion in this post here http://stackoverflow.com/questions/11839044/how-do-i-apply-uiappearance-proxy-properties-to-uilabel. I haven't tried it personally. – Adithya Jun 05 '13 at 10:23
  • To my knowledge UIlabel and UIButton conforms to the UIAppearanceContainer protocol but don't actually have any property marked with UI_APPEARANCE_SELECTOR – BooRanger Jun 05 '13 at 10:24
  • @Alladinian In fact in the Ray wenderlich article you have posted, its mentioned you should not customize UILabel using appearance proxy. – Adithya Jun 05 '13 at 10:30
  • 1
    @Filip XD, @Adi I wouldn't post the answer without testing it first. Maybe was a bug that is fixed now but `setTextColor:` **does** work for me. – Alladinian Jun 05 '13 at 10:33
  • [[UILabel appearance] setColor:[UIColor blueColor]];works for me i want to make headings with separate color and bold how to do that? – user2197875 Jun 05 '13 at 10:33
  • @user2197875 is `setTextColor:` not `setColor:` – Alladinian Jun 05 '13 at 10:34
  • 1
    @Adi [Ray's updated tutorial](http://www.raywenderlich.com/21703/user-interface-customization-in-ios-6) includes `UILabel` and [so does this one](http://useyourloaf.com/blog/2012/08/24/using-appearance-proxy-to-style-apps.html). Though it's pretty easy to give it a spin and see how it works. – Alladinian Jun 05 '13 at 10:41
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.

samir
  • 4,501
  • 6
  • 49
  • 76
  • can i change in appdelegate file – user2197875 Jun 05 '13 at 10:22
  • Why would you do this when you can just use [[UILabel appearance] setTextColor:[UIColor redColor]];? Which will change the text color of all UILabels. Please could you explain and give a reason to do it your way over using appearance? – Popeye Jun 05 '13 at 10:22
  • 1
    @Popeye because doing with UIAppareance is not flexible,imagine if one day he wants that one label will we redColor and all the others blue color :) – samir Jun 05 '13 at 10:31
  • That's fine you can just change it for that particular label, and that isn't what they have asked. Not going to downvote as it is still correct but I am not going to upvote ever as I wouldn't do it this way. – Popeye Jun 05 '13 at 10:34
1

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.

Adithya
  • 4,545
  • 3
  • 25
  • 28
  • Why would you do this when you can just use `[[UILabel appearance] setTextColor:[UIColor redColor]];`? Which will change the text color of all UILabels. Please could you explain and give a reason to do it your way over using `appearance`? – Popeye Jun 05 '13 at 10:22
  • @Popeye It seems there are some issues with using UIAppearence proxy on UILabel. Refer http://stackoverflow.com/a/11839198/1029360 – Adithya Jun 05 '13 at 10:25
  • @Popeye i couldn't get redcolor labels using ur statement – user2197875 Jun 05 '13 at 10:28
  • @Adi I can't say I have ever experienced any of these issues. I use it all the time and it works fine for me. I'm going to make the point that I'm not going to downvote as it is still correct, but I am not going to upvote ever as I wouldn't do it this way I would do it `Alladinians` way. – Popeye Jun 05 '13 at 10:29
  • @Popeye Yes, using appearance proxy for UILabel would had been the best method if not for this uncertainty. Hopefully it will be stable in ios7. – Adithya Jun 05 '13 at 10:37
  • @Adi Sorry I'm not sure I see your issue with `UIAppearence` I have never had any issues with it, it has always worked the way I want it to work. – Popeye Jun 05 '13 at 10:40