UILabel 中的多行文本
有没有办法像在 UITextView
中那样在 UILabel
中包含多行文本,或者我应该使用第二个文本?
Is there a way to have multiple lines of text in UILabel
like in the UITextView
or should I use the second one instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
将换行模式设置为自动换行,并将行数设置为
0
:恢复旧答案(供参考和愿意支持 6.0 以下 iOS 的开发人员):
另一方面:两个枚举值都为 <无论如何,代码> 0。
Set the line break mode to word-wrapping and the number of lines to
0
:Restored old answer (for reference and devs willing to support iOS below 6.0):
On the side: both enum values yield to
0
anyway.在 IB 中,将行数设置为 0(允许无限行)
使用 IB 在文本字段中输入时,使用“alt-return”插入回车符并转到下一行(或者您可以复制已由线)。
In IB, set number of lines to 0 (allows unlimited lines)
When typing within the text field using IB, use "alt-return" to insert a return and go to the next line (or you can copy in text already separated out by lines).
我发现的最佳解决方案(对于本来应该在框架中解决的令人沮丧的问题)类似于 vaychick 的解决方案。
只需在 IB 或代码中将行数设置为 0
这将显示所需的行,但会重新定位标签,使其水平居中(以便 1 行和 3 行标签在其水平位置对齐)。 要修复该问题,请添加:
The best solution I have found (to an otherwise frustrating problem that should have been solved in the framework) is similar to vaychick's.
Just set number of lines to 0 in either IB or code
This will display the lines needed but will reposition the label so its centered horizontally (so that a 1 line and 3 line label are aligned in their horizontal position). To fix that add:
使用它可以在 UILabel 中包含多行文本:
Swift:
Use this to have multiple lines of text in
UILabel
:Swift:
如果必须使用:
属性,您还可以在代码中使用标准换行符
("\n")
来强制换行。If you have to use the:
property you can also use a standard line break
("\n")
, in code, to force a new line.您可以使用
\r
转到下一行,同时使用NSString
填充UILabel
。You can use
\r
to go to next line while filling up theUILabel
usingNSString
.让我们试试这个
lets try this
上面的解决方案不适用于我的情况。 我正在这样做:
The solution above does't work in my case. I'm doing like this:
斯威夫特3
将动态文本信息的行数设置为零,这对于改变文本很有用。
Swift 3
Set number of lines zero for dynamic text information, it will be useful for varying text.
使用故事板:选择标签将行数设置为零......或者参考此
Use story borad : select the label to set number of lines to zero......Or Refer this
尝试使用这个:
Try using this:
由于某些原因,它在 iOS 6 中不适合我,不知道为什么。 尝试使用和不使用归因文本。 有什么建议。
For some reasons its not working for me in iOS 6 not sure why. Tried it with and without attributed text. Any suggestions.
方法1:
现在像这样简单地调用
EX:
方法2:
以编程方式
方法3:
通过故事板
显示多行设置0(零) ,这将在标签中显示多行。
如果要显示n行,则设置n。
请参见下面的屏幕。
如果您想设置标签的最小字体大小,请单击“自动收缩”并选择“最小字体大小”选项
请参阅下面的屏幕
这里设置最小字体大小
EX:9(在此图像中)
如果您的标签当时获得更多文本,您的标签文本将缩小至 9
Method 1:
Now call simply like this
EX:
Method 2:
Programmatically
Method 3:
Through Story board
To display multiple lines set 0(Zero), this will display more than one line in your label.
If you want to display n lines, set n.
See below screen.
If you want to set minimum font size for label Click Autoshrink and Select Minimum Font Size option
See below screens
Here set minimum font size
EX: 9 (In this image)
If your label get more text at that time your label text will be shrink upto 9
这些东西帮助我
更改了 UILabel 的这些属性
,并在提供输入字符串时使用 \n 在不同行中显示不同的单词。
例子 :
These things helped me
Change these properties of UILabel
And while giving input String use \n to display different words in different lines.
Example :
斯威夫特4:
Swift 4:
您也可以通过 Storyboard 执行此操作:
You can do that via the Storyboard too:
你应该尝试这个:
you should try this:
哦,2021年我被标签文本困住了1小时无法换行,然后意识到我忘记设置标签的宽度,WTF。
Oh, in 2021 I'm trapped by a label text unable to change lines for 1 hour, then realize I forget to set label's width, WTF.
已经回答了,但您也可以在情节提要中手动完成。 在标签的属性检查器下,您可以将换行符更改为自动换行(或字符换行)。
Already answered, but you can do it manually in the storyboard too. Under Attributes Inspector for the Label, you can change Line Breaks to Word Wrap (or character wrap).
在此函数中,传递要在标签中分配的字符串,并传递字体大小代替 self.activityFont,传递标签宽度代替 235,现在您可以根据字符串获取标签高度。
它会工作得很好。
In this function pass string that you want to assign in label and pass font size in place of self.activityFont and pass label width in place of 235, now you get label height according to your string.
it will work fine.
在代码或故事板本身中设置如下
Label.lineBreakMode = NSLineBreakByWordWrapping;
Label.numberOfLines = 0;
并且请不要忘记为标签设置左、右、上、下约束,否则它将不起作用。
Set below either in code or in storyboard itself
Label.lineBreakMode = NSLineBreakByWordWrapping;
Label.numberOfLines = 0;
and please don't forget to set left, right, top and bottom constraints for label otherwise it won't work.
在 C# 上,这在 UITableViewCell 中对我有用。
我认为这里的重点是:-
On C#, this worked for me inside UITableViewCell.
I think the point here is:-
此代码根据文本返回尺寸高度
This code is returning size height according to text