12

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?

fresskoma
  • 25,481
  • 10
  • 85
  • 128
Csabi
  • 3,097
  • 17
  • 59
  • 107
  • 1
    Possible duplicate of http://stackoverflow.com/questions/2265940/breakline-in-uibutton-title – fresskoma Jan 14 '11 at 15:44
  • See another question and some answers about this - [here](http://stackoverflow.com/questions/604632/how-do-you-add-multi-line-text-to-a-uibutton) – Chris Grant Jan 14 '11 at 15:44

2 Answers2

61

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;
RyuX51
  • 2,779
  • 3
  • 26
  • 33
PeyloW
  • 36,742
  • 12
  • 80
  • 99
4

Swift version for the checked response:

    myButton.titleLabel?.numberOfLines = 0
    myButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99