Pregunta

This is what I'm doing. I'm not dynamically creating the label, just picking from the object library.it is working only if I statically increase the height of the label, but I doesn't know at runtime

[self.lblDescription setText:@"This is going to be a test description but a very big description that should increase in height"];
[self.lblDescription sizeToFit];

The label is not going on the next line. But instead it is clipping it as soon as the left of the label reaches the right of the screen

Based on the answers and I tried the following but didn't work:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.lblDescription setText:@"This is going to be a test description but a very big description that should increase in size"];
    self.lblDescription.numberOfLines = 0;
    [self.lblDescription sizeToFit];
}

I also tried setting -

[self.lblDescription setLineBreakMode:NSLineBreakByWordWrapping];

before calling [self.lblDescription sizeToFit]; but didn't work

¿Fue útil?

Solución

Setting adjustsFontSizeToFitWidth property to true did it for me.

Try,

self.lblDescription.adjustsFontSizeToFitWidth = true

(by default it is set to false)

Otros consejos

Check for the numberOfLines property

self.lblDescription.numberOfLines = 0;

EDIT : Need to change the height as well (By the comment says) .

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              [UIFont fontWithName:@"Helvetica Neue" size:19], NSFontAttributeName,
                                              nil];
CGRect expectedLabelFrame = [text boundingRectWithSize:CGSizeMake(571, 500)
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                            attributes:attributesDictionary
                                               context:nil];
// Adjust the yourLabel the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelFrame.size.height;
yourLabel.frame = newFrame;

I've found that when using sizeToFit with a UILabel, in InterfaceBuilder you need to change the Autoshrink property from 'Fixed Font Size' to 'Minimum Font Size'. I usually then set its value to 0.5 to be sure its working properly.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top