开发者

Multiple Lines in UIButton

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

butto开发者_JAVA百科n = [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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜