UILabel text not automatically resized using Auto Layout
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.
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
As usual, thanks for any help with this.
- iOS: multiline uilabel only shows one line with autolayout
- Use Xcode Auto Layout for different image sizes on iPhone
- Alternative iOS layouts for portrait and landscape storyboard
- Xcode 6 Constraints with button images
- Xcode auto layout constraints - why defaulting to negative padding?
- UIImageView .scaleAspectFit and autolayout not working programmatically from Swift
3 Solutions Collect From Internet About “UILabel text not automatically resized using Auto Layout”
myLabel.adjustsFontSizeToFitWidth = YES; myLabel.minimumScaleFactor = .5f;
Then the label font size will be auto adjusted.
Along with the setting of
adjustsFontSizeToFitWidth and the
minimumScaleFactor, you also need to set the content compression of the label to have a low priority. Check out the “Compression Resistance and Content Hugging” section of this objc.io essay. Basically, if your label has a lower compression resistance priority than the trailing constraint, the label should get resized:
[myLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
I solved this problem by setting a required width constraint to the view I was linking the label to.
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0f constant:width];
And make sure the priority for this constraint is set to UILayoutPriorityRequired.
Text was resizing when pinning the label to the superview but not to the textfield, so I deduced there must be a problem with intrinsic sizes of elements involved in the layout calculation for the label’s final width.
- Terminology concerning UnsafeMutablePointer's destroy()
- iOS framework with dependencies
- Swift dynamic tableview in proportion to label height filled with text
- Type 'Any' has no subscript members after updating to Swift 3
- Autofill city and state from zip code in iOS
- Executing code, when the application is in background not working on physical device (SWIFT)
- iOS App crashing before entering main() with Xcode 4.2 & iOS 5
- iPhone/iOS: Will there be called any method if a UIView is added as a subview
- swift: How to load photos from photo library without using UIImagePickerController?
- Pass data through unwind segue
- Error in linking of a static iOS framework to both app's and XCTest targets
- var declaration with type vs without
- iOS: Warning “attempt to present ViewController whose view is not in the window hierarchy”
- Https iOS with self signed certificate
- How to download multiple images asynchronously in iOS without effect on UI?