简体   繁体   English

快速如何使ui标签总是4行

[英]swift how to make ui label always 4 lines

This is my code 这是我的代码

aboutMessageLabel.lineBreakMode = .ByWordWrapping
//                .ByWordWrapping // or NSLineBreakMode.ByWordWrapping
            aboutMessageLabel.numberOfLines = 4
            aboutMessageLabel.text = "bla bla"

where aboutmessageLabel is an instance of UILabel. 其中aboutmessageLabel是UILabel的实例。

If my content is more than 4 lines, it shows just the first 4 lines and that is what I want. 如果我的内容超过4行,则仅显示前4行,这就是我想要的。

However, if the content is less than 4 lines, it the height doesn't keep as the hieght for 4 lines, it becomes the hieght of the content. 但是,如果内容少于4行,则高度不会保持为4行的高度,而是内容的高度。

is there anyway where i can always make the label 4 lines even if the content is less than 4 lines? 无论如何,即使内容少于4行,我也总是可以使标签4行?

Set the size of the label in the MainStoryboard (like in the 2nd image), then click on your label, then in the Attributes Inspector set the number or Lines to 4 like this: MainStoryboard设置标签的大小(如第二张图片所示),然后单击标签,然后在Attributes Inspector中将数字或Lines数设置为4,如下所示:

在此处输入图片说明

Your label will have 4 lines, no matter who much text there is (which can be a bad thing as well if you have too much text like in the example): 无论有多少文本,标签都会有4行(如果示例中的文本太多,这也可能是一件坏事):

在此处输入图片说明

Furthermore, did you should add constraints to the label. 此外,您是否应在标签上添加约束。 This will keep it the same size always. 这将使其始终保持相同大小。 I'm assuming you want to create something like a meta description of a product, song, whatever. 我假设您想创建类似产品,歌曲等内容的元描述。 So if you keep the same size and have the 4 lines set, you'll be fine. 因此,如果您保持相同的大小并设置了4行,则可以。 It'll adjust based on the number of words while keeping the size. 它将根据单词数进行调整,同时保持大小。 Edit the color of the label background to see the if it works/stays the same size when testing. 编辑标签背景的颜色,以查看其在测试时是否起作用/保持相同的尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM