Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

How to break lines In UILabel?

I am trying to assign a text to the UILabel in UIViewController and my text is :

Get As Many Questions Right As You Can! Get A Questionn Wrong. And You Will Loose A Life! Lose All Three Lives And It's Game Over!

But when I insert it into label it looks like :

enter image description here

I try to set Lines = 4 Like this way :

enter image description here

But its not working and my output should look like :

enter image description here

How can I achieve that? Is there any other way to do this?

Thanks In advance.

Upvotes: 0

Views: 97

Answers (2)

pdeschain
pdeschain

Reputation: 1421

  • Do label.numberOfLines = 0; to allow for any number of lines.
  • Try to add \n where you need a line-break.
  • Update the label size to match the text size:

    CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:label.lineBreakMode];

    label.frame = CGRectMake( label.frame.origin.x, label.frame.origin.y, label.frame.size.width, labelSize.height);

Upvotes: 0

Rajeev Bhatia
Rajeev Bhatia

Reputation: 2994

Option 1:-

Add it to a TextView instead of a UILabel

Option 2:-

Change the number of lines in your UILabel to 0, set the auto layout constraints properly and set preferredMaxLayoutWidth as follows:-

lbl.preferredMaxLayoutWidth = self.view.bounds.width

Upvotes: 1

Related Questions