简体   繁体   中英

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

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..

so inside the UILabel it will look like:

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

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

// label.lineBreakMode = NSLineBreakMode.ByWordWrapping

// 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?

You could use UITextField instead which has some advantageous handling options

In order to wrap your text within a UILabel on Xcode 6 and above

  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
  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字段的屏幕抓图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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