0
  • iOS
  • Swift 3
  • XCode 8.2

Hello guys! How can I set the content of the label in order to display how much lines as it need but not cutting the single word. See the image attached above: in the example in the attachments, "Facebook" needs to be entirely displayed, and not displayed as "Facebo ok" or "FACEB OOK".

I mean, it's ok if the label will display the content in 1, 2, 3, n lines as much as it needs, but I don't want the it cut the single word if it's too long for the label's bounds, I just want that it resize every single word and when it face a space character then it can go in a new line.

I hope I've been clear at least to understand a little bit of my problem!

Thanks to everybody in advance!

Screenshot: enter image description here

Shobhakar Tiwari
  • 7,086
  • 2
  • 30
  • 65
Elia Crocetta
  • 296
  • 1
  • 6
  • 17
  • 1
    You should use Wordwrap in label. Refer http://stackoverflow.com/a/38653448/1423703 – Alok Rao Dec 13 '16 at 11:45
  • select Label from Interface builder and attribute inspector choose scale option ( minimum) and run – Shobhakar Tiwari Dec 13 '16 at 11:50
  • Alok Rao thanks, but as I said to the questions below, the word-wrap returns me the word cutted (it returns me "Faceb", not even "Facebo /n ok"). Shobhakar Tiwari already did, but didn't worked. – Elia Crocetta Dec 13 '16 at 14:19

3 Answers3

0

use this func

func boundingRect(with size: CGSize, 
          options: NSStringDrawingOptions = [], 
       attributes: [String : Any]? = nil, 
          context: NSStringDrawingContext?) -> CGRect

e.g.

 let sizeOfString = label.text.boundingRectWithSize(
                                 CGSizeMake(self.label.frame.size.width, CGFloat.infinity), 
                                 options: NSStringDrawingOptions.UsesLineFragmentOrigin, 
                                 attributes: [NSFontAttributeName: lbl.font], 
                                 context: nil).size

From here you get the size of the label and resize the frame of label according to you.

Dont forget to give >= contraint relation to the height of label and lines to 0 from storyboard.

dahiya_boy
  • 7,885
  • 1
  • 25
  • 39
0

If i understand correctly you just need to scale the content of the label so it fits the given bounds without character wrapping. If the above is true then try the code below to solve your problem. You will need to adjust the minimumScaleFacotr of the label so it can resize its content.

label.adjustsFontSizeToFitWidth = true // To adjust text to expand at all available space
label.minimumScaleFactor = 0.5 // The minimum scale factor that your label may reach to fit
label.lineBreakMode = NSLineBreakMode.byWordWrapping // Wrap at word boundaries
label.numberOfLines = 0 // For multiple lines till text fits`
chrilag
  • 31
  • 5
  • Thanks for your reply, but this function gives me the word at the default dimension, but cutted and no resized at all. For example it returns me "Faceb", even worse than the original problem I posted! :p Anyway thank you so much! – Elia Crocetta Dec 13 '16 at 14:17
0

You should simply write this code:

 Label.adjustsFontSizeToFitWidth = true
 Label.numberOfLines = 2  
 Label.text = "HelloHowAreYou? Fine."

I hope this code help you...

Pragnesh Vitthani
  • 2,302
  • 16
  • 27