Question

I just switched from a UILabel to UIButton, intending to use the button's titleLabel property to display the information formerly displayed in the label replaced by the button.

Problem is, the text doesn't show up. I've checked to see if the button itself (normally with a transparent background, changed to red to check) is appearing, and it is, but the text isn't there. Here's a sample of the relevant code, which is identical to the original (working) code from the label, changing only lines like:

UILabel *firstLabel;

(...)

firstLabel.textColor = [UIColor blackColor];

to:

UIButton *firstLabel;

(...)

firstLabel.titleLabel.textColor = [UIColor blackColor];

Here's a full chunk for clarity:

         firstLabel.frame = CGRectMake(thisRiser.bounds.origin.x, thisRiser.frame.size.height -focusBarEndHeight - 55, barWidth, 60);
         firstLabel.backgroundColor = [UIColor clearColor];
         firstLabel.titleLabel.textColor = [UIColor blackColor];
         firstLabel.titleLabel.text = [NSString stringWithFormat:@"%@\n%.2f%%\nTime--%@",focusItemName,focusItemPercent,actualDurationFocusItem];
         [firstLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
         firstLabel.titleLabel.numberOfLines = 0;
         firstLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
         [firstLabel setHidden:NO];

What am I missing? Any ideas?

Thanks!

UPDATE -- This may be related to a known bug!

Many thanks to the responders below. I implemented the first recommended fix, which resolved my initial problem, so my text now appears in the label. However, the UIControlStateHighlighted and UIControlStateSelected don't work.

For example, this code produces no discernible effect to the text when clicked:

firstLabel.backgroundColor = [UIColor clearColor];
[firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

After searching around SO for a while, I'm coming to the conclusion that there is a bug (at least in iOS 7) that prevents proper functioning of these methods. Unfortunately, I'm not smart enough to provide more detailed information, but plenty of recent posts point to a major problem in this area of UIControl.

I'll be ecstatic to be proven wrong, because I'd like to use this functionality.

Was it helpful?

Solution

UIButton responds to different messages, you can use the button state to change its title.

You can set the title like this;

UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:@"Title goes here" forState:UIControlStateNormal];

See Control State: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState

OTHER TIPS

To set a title in an UIButton, you have to init the button:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];

and then use:

[button setTitle:@"text" forState:UIControlStateNormal];

where you must specific the control state (highlighted, selected, etc.)

try for example,

UIButton *firstLabel = [UIButton buttonWithType:UIButtonTypeCustom];
firstLabel.frame = CGRectMake(0, 60, 100, 50); //set the frame
[firstLabel setTitle:@"Hello" forState:UIControlStateNormal];
[firstLabel setBackgroundColor:[UIColor redColor]];//for test
[firstLabel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; //it will always displays green for normal
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];//if u touch it text changed to white instantly
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];//if touch it and hold it shows white color


Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top