Reputation: 300
I am trying to change the text color of a label in Xcode IB. But each time, the background gets changed as well which is pretty annoying.
http://screencast.com/t/XgyqQrLe4cmV
What is the correct way to change only the text color?
Upvotes: 2
Views: 38806
Reputation: 1206
Also you can change the color to many other colors: Apple has quite a few convenience methods for this, say you wanted green text then you can use the greenColor convenience method:
label.textColor = [UIColor greenColor];
There are many options, here are the other convenience methods I found in "UIColor.h":
+ (UIColor *)blackColor; // 0.0 white
+ (UIColor *)darkGrayColor; // 0.333 white
+ (UIColor *)lightGrayColor; // 0.667 white
+ (UIColor *)whiteColor; // 1.0 white
+ (UIColor *)grayColor; // 0.5 white
+ (UIColor *)redColor; // 1.0, 0.0, 0.0 RGB
+ (UIColor *)greenColor; // 0.0, 1.0, 0.0 RGB
+ (UIColor *)blueColor; // 0.0, 0.0, 1.0 RGB
+ (UIColor *)cyanColor; // 0.0, 1.0, 1.0 RGB
+ (UIColor *)yellowColor; // 1.0, 1.0, 0.0 RGB
+ (UIColor *)magentaColor; // 1.0, 0.0, 1.0 RGB
+ (UIColor *)orangeColor; // 1.0, 0.5, 0.0 RGB
+ (UIColor *)purpleColor; // 0.5, 0.0, 0.5 RGB
+ (UIColor *)brownColor; // 0.6, 0.4, 0.2 RGB
+ (UIColor *)clearColor; // 0.0 white, 0.0 alpha
Alos if you wanted to color your label with a custom color not found in the above convenience methods like say Hot Pink we could do this: 255-105-180
label.textColor = [UIColor colorWithRed:(255.0/255.0) green:(105.0/255.0) blue:(180.0/255.0) alpha:1];//nice
RGB code for HOT pink I found here: http://www.tayloredmktg.com/rgb/
Also notice that I divide the RGB values by 255 to normalize them between 0 and 1. see this link for more info on that: Using [UIColor colorWithRed:green:blue:alpha:] doesn't work with UITableView seperatorColor?
I tested this out in Xcode 5, Deployment Target 7.1 and I got HOT PINK text! Now I'm ready for that Valentines app. try it your self :)
Upvotes: 2
Reputation: 9649
As simple as:
[self.lblValue setTextColor:[UIColor redColor]];
Upvotes: 1
Reputation: 119031
When you click the color box in the UI you will see it get selected. If you don't see it get selected, click it again. It's when it doesn't get selected that the colour changes the background instead (which seems to be the default selection if you keep the color picker open).
Upvotes: 2
Reputation: 8603
There should be a text color box in interface builder, but you can also do it through code.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,100)]
label.textColor = [UIColor redColor];
Or just use the reference that you already have to the text label. Make sure it is an IBOutlet and linked in interface builder though if you added it there.
Upvotes: 6