2

I am having a problem resizing the font size to fit a label and not splitting any words. I have a label that will change its content dynamically based on an array of strings. The label could have any number of lines. What I want is for the font size to be as big as possible while fitting the label AND not splitting any words. I made the font size very big on storyboard. The label is placed on the view using Auto-Layout.

For example for a string "Most definitely" if I use this code:

 answerLabel.numberOfLines = 0
 answerLabel.adjustsFontSizeToFitWidth = true
 answerLabel.lineBreakMode = .byTruncatingTail

I get strings like: Most(line 1) Definitel(line 2) y(line 3)

If on the other hand I do

    answerLabel.numberOfLines = 0
    answerLabel.adjustsFontSizeToFitWidth = true
    answerLabel.lineBreakMode = .byWordWrapping

I get a string "Mo" in big letters

Obviously what I am looking for in that case is Most(line 1) definitely(line 2).

Thanks in advance for any help.

Krunal
  • 77,632
  • 48
  • 245
  • 261
JP Aquino
  • 3,946
  • 1
  • 23
  • 25

1 Answers1

0

Swift 3
Set number of lines zero for dynamic text information, it will be useful for varying text.

var label = UILabel()
let stringValue = "Multiline UILabel not wrapping words correctly"
label.text = stringValue
label.numberOfLines = 0
label.lineBreakMode = .byTruncatingTail // or .byWrappingWord
label.minimumScaleFactor = 0.8 . // It is not required but nice to have a minimum scale factor to fit text into label frame

Using Interface Builder:

enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • This works for the "Most definitely" string but don't work for others. It still causes the words to be split. – JP Aquino Mar 02 '17 at 18:19
  • Please share your exact query, what are other words, and what's your design, so I can provide you solution accordingly... – Krunal Mar 02 '17 at 18:21