ios UILabel - 自动换行文本

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

UILabel - Wordwrap text

iosiphonecocoa-touchuilabelword-wrap

提问by Codezy

Is there any way to have a label wordwrap text as needed? I have the line breaks set to word wrap and the label is tall enough for two lines, but it appears that it will only wrap on line breaks. Do I have to add line breaks to make it wrap properly? I just want it to wrap if it can't fit it in horizontally.

有什么方法可以根据需要使用标签自动换行文本吗?我将换行符设置为自动换行,并且标签足够高,可以容纳两行,但它似乎只会在换行符上换行。我是否必须添加换行符才能使其正确换行?如果它不能水平放入,我只是想让它包裹起来。

回答by Kendall Helmstetter Gelner

If you set numberOfLinesto 0 (and the label to word wrap), the label will automatically wrap and use as many of lines as needed.

如果您设置numberOfLines为 0(并且标签为自动换行),标签将自动换行并根据需要使用尽可能多的行。

If you're editing a UILabelin IB, you can enter multiple lines of text by pressing option+returnto get a line break - returnalone will finish editing.

如果您UILabel在 IB 中编辑 a ,您可以通过按option+return来输入多行文本以获得换行符 -return单独完成编辑。

回答by Greg

UILabelhas a property lineBreakModethat you can set as per your requirement.

UILabel有一个属性lineBreakMode,您可以根据您的要求进行设置。

回答by Nathan

In Swift you would do it like this:

在 Swift 中,你会这样做:

    label.lineBreakMode = NSLineBreakMode.ByWordWrapping
    label.numberOfLines = 0

(Note that the way the lineBreakMode constant works is different to in ObjC)

(注意 lineBreakMode 常量的工作方式与 ObjC 中的不同)

回答by Naishta

Xcode 10, Swift 4

Xcode 10,斯威夫特 4

Wrapping the Text for a label can also be done on Storyboard by selecting the Label, and using Attributes Inspector.

通过选择标签并使用属性检查器,也可以在 Storyboard 上为标签包装文本。

Lines= 0Linebreak= Word Wrap

行数= 0换行符= 自动换行

enter image description here

在此处输入图片说明