简体   繁体   English

快速向UILabel添加多行文本

[英]Adding multiple Lines of text into UILabel in swift

Hi I want to display some text in my UILabel line by line when an action is occurred 嗨,我想在发生动作时在UILabel中逐行显示一些文本

Hi, I have a textbox, button and a UILabel, lets say I've typed in "This text will be displayed line by line in a UILabel" and pressed the button to display the text in the UILabel.. 嗨,我有一个文本框,按钮和一个UILabel,可以说我输入了“此文本将在UILabel中逐行显示”,然后按下按钮以在UILabel中显示文本。

so inside the UILabel it will look like: 因此在UILabel内部,它看起来像:

This
text
will
be
displayed
line
by
line
in
a
UILabel

.... ....

I've been searching forums but could not found an answer.. so I don't know if this is possible.. please help ps I've set number of lines to 0 and line breaks is set to word wrap 我一直在搜索论坛,但找不到答案..所以我不知道这是否可能..请帮助ps我已将行数设置为0,并将换行符设置为自动换行

let label = UILabel(frame: CGRect(x: 100, y: 100, width: 100, height: 300)) // make the height biggest 

// label.lineBreakMode = NSLineBreakMode.ByWordWrapping // label.lineBreakMode = NSLineBreakMode.ByWordWrapping

// label.numberOfLines = 0 // label.numberOfLines = 0
dont work if label width bigger word width 标签宽度大于字宽时不起作用

var string = "This text will be displayed line by line in a UILabel"

var array = string.componentsSeparatedByString(" ")

var newstring = join("\r", array)
label.text = newstring
 let labelText = "Write\rthe\rtext\rwith\rline\rbreaks."

Does it have to be a UILabel? 是否必须是UILabel?

You could use UITextField instead which has some advantageous handling options 您可以改用UITextField,它具有一些有利的处理选项

In order to wrap your text within a UILabel on Xcode 6 and above 为了在Xcode 6及更高版本上的UILabel中包装文本

  1. Select the label you are working with. 选择您正在使用的标签。
  2. Navigate to the Utilities panel. 导航到“实用程序”面板。
  3. Select the "Attributes inspector" 选择“属性检查器”
  4. Select the number of lines you would like to display on the label (2,3,4) depending on the size of your UILabel field 选择要在标签上显示的行数(2,3,4),具体取决于UILabel字段的大小
  5. Select "Word Wrap" under Line Breaks, or you can select any option of your preference. 选择“换行”下的“自动换行”,或者您可以选择自己喜欢的任何选项。 You may manipulate the way your text appears to your taste. 您可以按照自己的喜好操纵文本的显示方式。

Xcode字段的屏幕抓图

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

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