Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
           action:@selector(myButtonClick) 
 forControlEvents:UIControlEventTouchDown];

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?

like image 781
Csabi Avatar asked Jan 14 '11 15:01

Csabi


2 Answers

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:

ObjC:

myButton.titleLabel.numberOfLines = 0; // Dynamic number of lines
myButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;

Swift:

myButton.titleLabel?.numberOfLines = 0; // Dynamic number of lines
myButton.titleLabel?.lineBreakMode = .byWordWrapping;
like image 114
PeyloW Avatar answered Oct 18 '22 08:10

PeyloW


Swift version for the checked response:

    myButton.titleLabel?.numberOfLines = 0
    myButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
like image 31
Kevin ABRIOUX Avatar answered Oct 18 '22 08:10

Kevin ABRIOUX