标题中包含两行文本的 UIButton (numberOfLines = 2)

我试图创建一个 UIButton,它的 title Label 中有两行文本。这是我正在使用的代码:

UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
[titleButton setTitle:@"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleButton.titleLabel.numberOfLines = 2;
[self addSubview:titleButton];

当我尝试这样做时,文本只出现在一行上。在 UIButton.titleLabel中实现多行文本的唯一方法似乎是设置 numberOfLines=0并使用 UILineBreakModeWordWrap。但这并不能保证文本正好是两行。

然而,使用普通的 UILabel确实有效:

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = @"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:titleLabel];


    

有人知道如何让 UIButton用两条线工作吗?创建一个单独的 UILabel来保存文本,并将其添加为按钮的子视图,这是唯一的解决方案吗?

71140 次浏览

Updated answer for more recent iOS versions

Since this is the accepted answer, added @Sean's answer here:

Set these properties on the titleLabel of your button.

button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2; // if you want unlimited number of lines put 0

Swift 3 and 4:

button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.numberOfLines = 2 // if you want unlimited number of lines put 0

Original answer for an older version of iOS

If you want 2 lines of text on top of your UIButton you should add a UIlabel on top of it that does precisely that.

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = @"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[myButton addSubview:titleLabel]; //add label to button instead.

Updated for interface builder solution

Added @Borut Tomazin's answer for a more complete answer. Updated this part again since the answer of @Borut Tomazin was improved.

You can do this much easier, with no code required. In Interface Builder set Line Break on UIButton to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line. You will also need to add this to the User Defined Runtime Attribute in Interface Builder:

titleLabel.textAlignment Number [1]

You can do this much easier, with no code required. In Interface Builder set Line Break on UIButton to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line. You will also need to add this to the User Defined Runtime Attribute in Interface Builder:

titleLabel.textAlignment Number [1]

It's that simple. Hope it helps...

You don't need to add a UILabel to the UIButton. That's just extra objects and work.

Set these properties on the titleLabel of your button.

button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2;//if you want unlimited number of lines put 0

Swift:

button.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
button.titleLabel!.numberOfLines = 2//if you want unlimited number of lines put 0
 button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;


button.titleLabel.textAlignment = NSTextAlignmentCenter;


[button setTitle: @"Line1\nLine2" forState: UIControlStateNormal];

To avoid completely the need to edit code, and thus the need to subclass your view, in Xcode5 and greater you can follow Borut Tomazin suggestion:

In Interface Builder (or storyboard) set Line Break to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line.

and then, in the User Defined Runtime Attributes you can add

Key path: titleLabel.textAlignment
Type: Number
Value: 1

Note: this may be not completely "future proof" since we are translating the UITextAlignmentCenter constant into its numerical value (and that constant may change as new iOS versions are released), but it seems safe in the near future.

You can modify the needed value directly from Storyboard. Select the button, go to the identity inspector and add the following key-value pair in the "User defined runtime attributes" section:

enter image description here