Can not change UILabel text color
Asked Answered
T

6

66

I want to change the UILabel text color but I can't change the color, This is how my code looks like.

UILabel *categoryTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 46, 16)];
categoryTitle.text = @"abc";
categoryTitle.backgroundColor = [UIColor clearColor];
categoryTitle.font = [UIFont systemFontOfSize:12];
categoryTitle.textAlignment = UITextAlignmentCenter;
categoryTitle.adjustsFontSizeToFitWidth = YES;
categoryTitle.textColor = [UIColor colorWithRed:188 green:149 blue:88 alpha:1.0];
[self.view addSubview:categoryTitle];
[categoryTitle release];

The label text color is white ,not my custom color.

Thank for any help.

Theron answered 28/3, 2010 at 9:20 Comment(1)
if you get confused on this, as a test use .. [UIColor greenColor], [UIColor yellowColor] and so on.Hoxsie
L
177

UIColor's RGB components are scaled between 0 and 1, not up to 255.

Try

categoryTitle.textColor = [UIColor colorWithRed:(188/255.f) green:... blue:... alpha:1.0];

In Swift:

categoryTitle.textColor = UIColor(red: 188/255.0, green: ..., blue: ..., alpha: 1)
Leilanileininger answered 28/3, 2010 at 9:21 Comment(3)
Hi @KennyTM.Can I know why we have to use divide our value with 255Mercier
@EXC_BAD_ACCESS: To convert the values from 0 ... 255 (0xff) to 0.0 ... 1.0Leilanileininger
If we already done this division and having those 0.5,0.6,0.3 for RGB respectively, How can we give that value.Mercier
A
8

May be the better way is

UIColor *color = [UIColor greenColor];
[self.myLabel setTextColor:color];

Thus we have colored text

Afghanistan answered 1/6, 2013 at 12:25 Comment(3)
The OP specifically wanted to use a custom colour, not just a preset colour, so using a preset is not a viable solution. The accepted answer is the correct one.Rattletrap
The OP should not use a custom colour, it's too hard :)Hoxsie
"Too hard"? God... (keeping sarcasm up!)Mccray
K
2

Try this one, where alpha is opacity and others is Red,Green,Blue chanels-

self.statusTextLabel.textColor = [UIColor colorWithRed:(233/255.f) green:(138/255.f) blue:(36/255.f) alpha:1];
Kao answered 18/5, 2015 at 14:26 Comment(1)
Some explanation would be nice!Imperishable
M
1

It is possible, they are not connected in InterfaceBuilder.

Text colour(colorWithRed:(188/255) green:(149/255) blue:(88/255)) is correct, may be mistake in connections,

backgroundcolor is used for the background colour of label and textcolor is used for property textcolor.

Monophyletic answered 3/7, 2014 at 6:45 Comment(0)
T
1

Add attributed text color in swift code.

Swift 4:

  let greenColor = UIColor(red: 10/255, green: 190/255, blue: 50/255, alpha: 1)
  let attributedStringColor = [NSAttributedStringKey.foregroundColor : greenColor];

  let attributedString = NSAttributedString(string: "Hello World!", attributes: attributedStringColor)
  label.attributedText = attributedString

for Swift 3:

  let greenColor = UIColor(red: 10/255, green: 190/255, blue: 50/255, alpha: 1)
  let attributedStringColor : NSDictionary = [NSForegroundColorAttributeName : greenColor];


  let attributedString = NSAttributedString(string: "Hello World!", attributes: attributedStringColor as? [String : AnyObject])
  label.attributedText = attributedString 
Trimeter answered 5/7, 2018 at 8:56 Comment(1)
Good way to set the attribute for Label.Mightily
G
0
// This is wrong 
categoryTitle.textColor = [UIColor colorWithRed:188 green:149 blue:88 alpha:1.0];

// This should be  
categoryTitle.textColor = [UIColor colorWithRed:188/255 green:149/255 blue:88/255 alpha:1.0];

// In the documentation, the limit of the parameters are mentioned.

colorWithRed:green:blue:alpha: documentation link

Gemmell answered 5/7, 2018 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.