ios 标题中有两行文本的 UIButton (numberOfLines=2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7831928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:57:30  来源:igfitidea点击:

UIButton with two lines of text in the title (numberOfLines=2)

objective-ciosuibuttonuilabel

提问by marketer

I'm trying to make a UIButtonthat has two lines of text in its titleLabel. This is the code I'm using:

我正在尝试制作一个UIButton在其 titleLabel 中有两行文本的文件。这是我正在使用的代码:

    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];

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.titleLabelis to set numberOfLines=0and use UILineBreakModeWordWrap. But this doesn't guarantee the text to be exactly two lines.

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

Using a plain UILabel, however, does work:

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];

Does anyone know how to make the UIButtonwork with two lines? Is the only solution to create a separate UILabelto hold the text, and add it as a subview of the button?

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

采纳答案by Totumus Maximus

Updated answer for more recent iOS versions

更新了最新 iOS 版本的答案

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

由于这是公认的答案,因此在此处添加了@Sean 的答案:

Set these properties on the titleLabel of your button.

在按钮的 titleLabel 上设置这些属性。

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

Swift 3 and 4:

斯威夫特 3 和 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

旧版 iOS 的原始答案

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

如果你想在UIButton你的UIlabel顶部添加2 行文本,你应该在它上面添加一个正好可以做到这一点的文本。

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.

添加了@Borut Tomazin 的答案以获得更完整的答案。自从@Borut Tomazin 的答案得到改进后,再次更新了这一部分。

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

您可以更轻松地完成此操作,无需任何代码。在 Interface Builder 中将Line BreakUIButton设置为Word Wrap. 比你可以插入多行标题。只需Option + Return按键即可创建新行。您还需要将此添加到界面生成器中的用户定义的运行时属性:

titleLabel.textAlignment Number [1]

回答by Sean

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

您不需要向 UIButton 添加 UILabel。那只是额外的对象和工作。

Set these properties on the titleLabel of your button.

在按钮的 titleLabel 上设置这些属性。

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

回答by Borut Tomazin

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

您可以更轻松地完成此操作,无需任何代码。在 Interface Builder 中将Line BreakUIButton设置为Word Wrap. 比你可以插入多行标题。只需Option + Return按键即可创建新行。您还需要将此添加到界面生成器中的用户定义的运行时属性:

titleLabel.textAlignment Number [1]

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

就这么简单。希望能帮助到你...

回答by Suraj K Thomas

 button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;

button.titleLabel.textAlignment = NSTextAlignmentCenter;

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

回答by furins

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:

为了完全避免编辑代码的需要,从而避免需要子类化您的视图,在 Xcode5 和更高版本中,您可以遵循 Borut Tomazin 的建议:

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

在 Interface Builder (或 storyboard)中,将 Line Break 设置为 Word Wrap。比你可以插入多行标题。只需按 Option+Return键即可创建新行。

and then, in the User Defined Runtime Attributesyou can add

然后,在用户定义的运行时属性中,您可以添加

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

关键路径:titleLabel.textAlignment
类型:Number
值:1

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

注意:这可能不是完全“面向未来的证明”,因为我们正在将UITextAlignmentCenter常量转换为其数值(并且该常量可能会随着新 iOS 版本的发布而变化),但在不久的将来似乎是安全的。

回答by Arik Segal

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:

您可以直接从 Storyboard 修改所需的值。选择按钮,转到身份检查器并在“用户定义的运行时属性”部分添加以下键值对:

enter image description here

在此处输入图片说明