Created
June 17, 2013 20:49
-
-
Save bentford/5800254 to your computer and use it in GitHub Desktop.
Shows how to implement a multiline UILabel with Auto Layout. It will dynamically resize to fit whatever horizontal constraints you apply.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface AutoLayoutLabel : UILabel | |
@end | |
@implementation AutoLayoutLabel | |
{ | |
CGFloat overrideWidth; | |
} | |
- (id)init | |
{ | |
if ((self = [super init])) { | |
overrideWidth = 0.0f; | |
self.numberOfLines = 0; | |
self.lineBreakMode = NSLineBreakByWordWrapping; | |
} | |
return self; | |
} | |
- (void)layoutSubviews | |
{ | |
[super layoutSubviews]; | |
// Fit the label to it's contents on rotation or text change | |
// http://stackoverflow.com/questions/15927086/uilabel-sizetofit-and-constraints | |
if (self.preferredMaxLayoutWidth != [self alignmentRectForFrame:self.frame].size.width) { | |
self.preferredMaxLayoutWidth = [self alignmentRectForFrame:self.frame].size.width; | |
[self.superview setNeedsLayout]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems that overrideWidth is not used anywhere