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
UILabel - Wordwrap text
提问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 numberOfLines
to 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 UILabel
in 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
UILabel
has a property lineBreakMode
that 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 中的不同)