views:

396

answers:

3

Everything about this UIButton renders great except the text that's supposed to be on it. NSLog demonstrates that the text is in the right place. What gives?

UIButton *newTagButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[newTagButton addTarget:self action:@selector(showNewTagField) forControlEvents:UIControlEventTouchUpInside];
newTagButton.titleLabel.text = @"+ New Tag";
NSLog(@"Just set button label to %@", newTagButton.titleLabel.text);
newTagButton.titleLabel.font = [UIFont systemFontOfSize:17];
newTagButton.titleLabel.textColor = [UIColor redColor];
CGSize addtextsize = [newTagButton.titleLabel.text sizeWithFont:[UIFont systemFontOfSize:17]];
CGSize buttonsize = { (addtextsize.width + 20), (addtextsize.height * 1.2) };
newTagButton.frame = CGRectMake(x, y, buttonsize.width, buttonsize.height);
[self.mainView addSubview:newTagButton];
A: 

Did you remember to link the IBOutlet in interface builder? :-)

Mike Howard
No interface builder; I'm building it in code. And it renders fine, just with no text on it.
Dan Ray
+3  A: 

There are a set of APIs on UIButton that should be used to change those properties.

The titleLabel can and will be changed by the UIButton internally.

[button setTitle:title forState:state];
[button setTitleColor:color forState:state];
[button setTitleShadowColor:color forState:state];

You should always set these properties through these methods (when available) rather than touching the titleLabel directly. For fonts you can change it on the titleLabel directly since they don't provide a method on UIButton.

Maven
Bingo. Thank you!
Dan Ray
A: 

(Once again, no one can 'comment' here.)

=> rather than touching the titleLabel directly

How about GETTING text that's currently in a button?

Should I use "forState" or just get the text directly?

Gloria
You can just use the currentTitle property to get the current text of the button.
Maven
+1 Maven--those properties are read-only on a UIButton, it turns out. So read your heart out on 'em!
Dan Ray