界面生成器中的 iOS 多行标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6557488/
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 20:36:50  来源:igfitidea点击:

iOS multiline label in Interface builder

iosobjective-cswiftuilabelline-breaks

提问by Samuli Lehtonen

How can I make a multiline UILabelin interface builder for iOS? I tried the UITextViewbut it didn't quite suit my needs.

如何UILabel在 iOS 的界面构建器中制作多行?我试过了,UITextView但它不太适合我的需要。

How can I add multiline (text) in label?

如何在标签中添加多行(文本)?

回答by akashivskyy

You can use numberOfLinesproperty which defines maximumnumber of lines a label can have. By default, it's 1. Setting it to 0means the label will have unlimited lines.

您可以使用numberOfLines定义标签可以拥有的最大行数的属性。默认情况下,它是1. 将它设置为0意味着标签将有无限行

You can do it in code:

你可以在代码中做到:

textLabel.numberOfLines = 5 // for example

Or in Interface Builder:

或者在界面生成器中:

回答by user1233894

Hit Control+Enterto add a line in UILabel in Interface Builder/Storyboard.

点击Control+Enter在界面生成器/故事板的 UILabel 中添加一行。

回答by Bogdan

Thanks AppleVijay!

感谢 AppleVijay!

Also to call sizeToFit, like this:

也可以调用 sizeToFit,如下所示:

label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
[label sizeToFit];

The height will be automatically computed.

高度将自动计算。

回答by Vijay-Apple-Dev.blogspot.com

set width of label as u needed small then use IB to set line breaks to word wrap

将标签的宽度设置为小,然后使用 IB 将换行符设置为自动换行

or use with code like this

或与这样的代码一起使用

I found a solution.

我找到了解决方案。

One just has to add the following code:

只需添加以下代码:

textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;

回答by Krunal

Set number of lines zero for dynamic text information, it will be useful when your text are varying.

将动态文本信息的行数设置为零,这在您的文本变化时会很有用。

Programatically (Swift 3)

以编程方式 (Swift 3)

var label = UILabel()
let stringValue = "iOS\nmultiline\nlabel\nin\nInterface\nbuilder"
label.text = stringValue
label.numberOfLines = 0 // Set 0, if number of lines not specified.
label.lineBreakMode = .byTruncatingTail // or .byWrappingWord
label.minimumScaleFactor = 0.8 . // It is not required but nice to have a minimum scale factor to fit text into label frame


Using Inetrface Builder

使用接口生成器

enter image description here

在此处输入图片说明

Note: It is not required to set Minimum Font Scale, but nice to have a minimum scale factor to fit text into label frame.

注意:不需要设置Minimum Font Scale,但最好有一个最小比例因子来将文本放入标签框架中。

回答by siddhant

Number of lines is visible in IB with Plain UILabels set lines field as 0 . It will create multiple lines as per the provided space to the label.

行数在 IB 中可见,Plain UILabels 将行字段设置为 0 。它将根据为标签提供的空间创建多行。

回答by Artisan

In iOS7 (Xcode5) you shold set the lines of UILabel to 0for unlimited multiple input in storyboard.
The most important is to set the height of the UILabelcan hold the lines of input you are going to set.

在 iOS7 (Xcode5) 中,您应该将 UILabel 的行设置0为故事板中的无限多个输入。
最重要的是设置height of the UILabel可以容纳您要设置的输入行。

回答by Tanvi Jain

textLabel.lineBreakMode = UILineBreakModeWordWrap;

textLabel.numberOfLines = 0;

CGSize size =  [[[arrNewsFeed objectAtIndex:row] objectForKey:@"c"]  sizeWithFont:[UIFont systemFontOfSize:14.0]  constrainedToSize:CGSizeMake(188, CGFLOAT_MAX)
                                                                     lineBreakMode:NSLineBreakByTruncatingTail];

textLabel.frame = (CGRect){.origin = cell.lblNewsDescription.frame.origin, .size = size};

回答by Harshal Karande

I had been struggling with this for a long time. I just wanted my label text to appear on the second line. But whatever I do, it would just overflow the UILabel box. For me, changing the autoresizing in size inspector worked. Simple fix.

我已经为此苦苦挣扎了很长时间。我只是想让我的标签文本出现在第二行。但无论我做什么,它都会溢出 UILabel 框。对我来说,更改大小检查器中的自动调整大小有效。简单的修复。

May be someone might find it helpful who is looking for something similar.

可能有人会发现正在寻找类似内容的人会有所帮助。

回答by Pablo Marrufo

If you set the numberOfLines property equal to 0, the label will automatically adjust to the required number of lines of the given text.

如果将 numberOfLines 属性设置为 0,标签将自动调整到给定文本所需的行数。