user4276168
user4276168

Reputation:

MultiLine UIlabel doesn't show all text?

I want to show label size as per text in it for that I have used uilabel number of line property.

lbl.numberoflines = 0;

but it shows only three line and after that it shows ... . not all text showing.

when I will give number of line more than 3 then it showing that line in label.

appreciate for help

Upvotes: 3

Views: 4106

Answers (5)

Swasidhant
Swasidhant

Reputation: 1251

So I had made numberOfLines as 0 and had given lineBreakMode as WordWrap and had given proper constraints too.

What worked was changing the preferred width setting to automatic from explicit. i.e. from this:-

enter image description here

to this:-

enter image description here

Upvotes: 2

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

try this

lbl.lineBreakMode = UILineBreakModeWordWrap;

lbl.numberOfLines = 0;

if you want to manually calculate the height

 lbl.numberOfLines = 0; // allows label to have as many lines as needed
lbl.text =@"xxxxxxxxxxxxxxxxxxxxxx";
CGSize labelSize = [ lbl.text sizeWithFont: lbl.font constrainedToSize:CGSizeMake(300, 300)  lineBreakMode:NSLineBreakByWordWrapping];

// set the frame of labels here 

else you can directly placed in Attribute

enter image description here

swift3

lbl.lineBreakMode = .byWordWrapping
lbl.numberOfLines = 0

// allows label to have as many lines as needed
lbl.text = "xxxxxxxxxxxxxxxxxxxxxx"
var labelSize = lbl.text.size(with: lbl.font, constrainedToSize: CGSize(width: 300, height: 300), lineBreakMode: .byWordWrapping)
// set the frame of labels here  }

Upvotes: 6

NSSwift
NSSwift

Reputation: 215

All answers may helpful. but uses old approach.

can't you add Constraint to your view ?

It will be really easy to set layout constraint, and then you go.

Upvotes: 0

Kumar
Kumar

Reputation: 1942

Use this method. May Help

-(CGSize)getLabelSize:(NSString *)text
{

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
    CGSize labelSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    return labelSize;

}

Upvotes: 0

sohil
sohil

Reputation: 848

try this

lbl.lineBreakMode = UILineBreakModeWordWrap;
lbl.numberOfLines = 0;
[lbl sizeTofit];

Upvotes: 0

Related Questions