Thursday, May 30, 2013

UIButton titleLabel example in Objective C (iOS).

UIButton titleLabel

A view that displays the value of the currentTitle property for a button. (read-only)

@property(nonatomic, readonly, retain) UILabel *titleLabel

Discussion of [UIButton titleLabel]
Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:

UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.

The titleLabel property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.

UIButton titleLabel example.
// Create a temporary NSString to store the new formatted text string
// Set it to the first character so we can have a simple loop from the second to the last character
NSString *newText = [NSString stringWithFormat:@"%C",[button.titleLabel.text characterAtIndex:0]];
for(int i=1;i

Example of [UIButton titleLabel].
myButton.titleLabel.text = @"this is the new label";
[myButton setNeedsLayout];

UIButton titleLabel example.
button.titlelabel.minimumFontSize = 8.0; // or some more adequate size
self.buttonWithLongTitle.titleLabel.adjustsFontSizeToFitWidth = YES;

End of UIButton titleLabel example article.