UIButton标题中有两行文字(numberOfLines = 2) [英] UIButton with two lines of text in the title (numberOfLines=2)
问题描述
我正在尝试制作一个 UIButton
,其titleLabel中有两行文字。这是我正在使用的代码:
I'm trying to make a UIButton
that has two lines of text in its titleLabel. This is the code I'm using:
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
。但是这并不能保证文本正好是两行。
When I try this, the text only appears on one line. It seems the only way to achieve more than one line of text in UIButton.titleLabel
is to set numberOfLines=0
and use UILineBreakModeWordWrap
. But this doesn't guarantee the text to be exactly two lines.
然而,使用普通的 UILabel
work:
Using a plain UILabel
, however, does work:
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
来保存文本,并将其添加为按钮的子视图?
Does anyone know how to make the UIButton
work with two lines? Is the only solution to create a separate UILabel
to hold the text, and add it as a subview of the button?
推荐答案
更新了近期iOS版本的答案
由于这是接受的答案,因此添加了@ Sean的答案在这里:
Since this is the accepted answer, added @Sean's answer here:
在按钮的titleLabel上设置这些属性。
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和4:
Swift 3 and 4:
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.numberOfLines = 2 // if you want unlimited number of lines put 0
旧版本的原始答案iOS
如果你想在 UIButton
之上加2行文字,你应该添加一个 UIlabel
就在它之上。确实如此。
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.
已更新界面构建器解决方案
添加了@Borut Tomazin的答案以获得更完整的答案。
由于@Borut Tomazin的答案得到了改进,所以再次更新了这一部分。
Added @Borut Tomazin's answer for a more complete answer. Updated this part again since the answer of @Borut Tomazin was improved.
您可以更轻松地完成这项工作,无需代码。在Interface Builder中,将UIButton上的换行符
设置为 Word Wrap
。比你可以插入多行标题。只需按 Option + Return
键即可创建新行。您还需要将其添加到Interface Builder中的用户定义的运行时属性:
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]
这篇关于UIButton标题中有两行文字(numberOfLines = 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!