本文介绍了标题中有两行文本的 UIButton (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 确实有效:

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 上的 Line Break 设置为 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:54