Csabi Csabi - 1 year ago 95
Objective-C Question

Multiple Lines in UIButton

Hi I have problem for set multiple lines to my Button which is declared like that:

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button.titleLabel.numberOfLines = 0;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);

[button addTarget:self

button.frame = CGRectMake(0.0, 100.0, 317.0, 100.0);
[button setTitle:string forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.text = @"ahoj";

NSMutableString *ObratString = [[NSMutableString alloc] initWithString:button.titleLabel.text];

[ObratString appendString:@"\n"];
[ObratString appendString:@"caw"];
[ObratString appendString:@"\n"];
[ObratString appendString:@"helllo"];
button.titleLabel.text = ObratString;
[ObratString release];
[self.view addSubview:button];

But in the end I just see the first line.
Is there any way to make it work?

Answer Source

The UIButton displays it's text with a contained UILabel. The default for the contained label is to display one line of text only. This label is accessible through the titleLabel property, and anything you can do to a normal label can be done to it.

For example making it multi-lines broken by words:

 myButton.titleLabel. numberOfLines = 0; // Dynamic number of lines
 myButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download