Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to implement a constrained UITableViewCell subclass and everything is working perfectly, except for the UILabel. The constraints that I've set up are definitely being enforced, but the text inside of the label does not resize to a smaller font size when the constraints clash. Instead, the height of the UILabel gets truncated and the font remains the same size, meaning the letters get cut off at the top and bottom.

Is there some method I have to call in order to get this to work? I would think Auto Layout would be smart enough to automatically resize the font size, so I'm kind of lost as to why this is happening.

Relevant Code:

self.label = [[UILabel alloc] initWithFrame:CGRectZero];
self.label.textColor = [UIColor whiteColor];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.numberOfLines = 1;
[self.contentView addSubview:self.label];

NSLayoutConstraint *otherViewToLabelHorizontalConstraint =  // Make sure that the label is always to the right of the other view.
                    [NSLayoutConstraint constraintWithItem:self.label 
                                                 attribute:NSLayoutAttributeLeft 
                                                 relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                    toItem:self.otherView 
                                                 attribute:NSLayoutAttributeRight 
                                                multiplier:1.0
                                                  constant:0.0];

NSLayoutConstraint *aTextFieldToLabelVerticalConstraint = 
                    [NSLayoutConstraint constraintWithItem:self.label 
                                                 attribute:NSLayoutAttributeTop 
                                                 relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                    toItem:self.aTextField 
                                                 attribute:NSLayoutAttributeBottom 
                                                multiplier:1.0
                                                  constant:0.0];

Basically, these constraints are meant to enforce a cell where otherView is on the left, aTextField is on the right of otherView at the same y-level, and the label is below aTextField and to the right of the bottom of otherView.

As usual, thanks for any help with this.

share|improve this question
Are you getting an exception or some other log message in your console when adding these constraints? – larsacus 3 hours ago
Nope, I ended up getting rid of the parts that used this constraint, though. – J Kagney 2 hours ago

1 Answer

You need

myLabel.adjustsFontSizeToFitWidth = YES;
myLabel.minimumScaleFactor = .5f;

Then the label font size will be auto adjusted.

share|improve this answer
Thanks for the response, but after implementing this code, it still remains cut off at the top and bottom. I have the font sized as 18.0f. I've tested the font and the minimum it needs to fit is 13.0f, so I don't understand why it wouldn't be automatically scaling it down until the 0.5f multiplier is reached. – J Kagney yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.