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?
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;
Swift version for the checked response:
myButton.titleLabel?.numberOfLines = 0
myButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With