change UILabel's text color for total project
Asked Answered
S

3

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 .

Style answered 5/6, 2013 at 10:6 Comment(1)
Are you targeting iOS 5+ ?Karame
K
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

Karame answered 5/6, 2013 at 10:17 Comment(10)
Blue is prettier: [[UILabel appearance] setColor:[UIColor blueColor]]; ^^Grosberg
Changed my mind: [[UILabel appearance] setColor:[UIColor purpleColor]];Grosberg
+1 simple and affective. Not sure why you would do it any other way.Haug
It seems there could be some issues using this method according to discussion in this post here #11839544. I haven't tried it personally.Andriaandriana
To my knowledge UIlabel and UIButton conforms to the UIAppearanceContainer protocol but don't actually have any property marked with UI_APPEARANCE_SELECTORParticiaparticipant
@Karame In fact in the Ray wenderlich article you have posted, its mentioned you should not customize UILabel using appearance proxy.Andriaandriana
@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.Karame
[[UILabel appearance] setColor:[UIColor blueColor]];works for me i want to make headings with separate color and bold how to do that?Style
@Style is setTextColor: not setColor:Karame
@Adi Ray's updated tutorial includes UILabel and so does this one. Though it's pretty easy to give it a spin and see how it works.Karame
A
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.

Allethrin answered 5/6, 2013 at 10:18 Comment(4)
can i change in appdelegate fileStyle
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?Haug
@Haug 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 :)Allethrin
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.Haug
A
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.

Andriaandriana answered 5/6, 2013 at 10:13 Comment(6)
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?Haug
@Haug It seems there are some issues with using UIAppearence proxy on UILabel. Refer https://mcmap.net/q/340304/-how-do-i-apply-uiappearance-proxy-properties-to-uilabelAndriaandriana
@Haug i couldn't get redcolor labels using ur statementStyle
@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.Haug
@Haug Yes, using appearance proxy for UILabel would had been the best method if not for this uncertainty. Hopefully it will be stable in ios7.Andriaandriana
@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.Haug

© 2022 - 2024 — McMap. All rights reserved.