Skip to content

Instantly share code, notes, and snippets.

@WJachowicz
Created May 19, 2014 07:42
Show Gist options
  • Save WJachowicz/cada29a14b521ffaba62 to your computer and use it in GitHub Desktop.
Save WJachowicz/cada29a14b521ffaba62 to your computer and use it in GitHub Desktop.
@implementation VCCMultilineLabel
- (id)init
{
if ((self = [super init])) {
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];
}
}
- (CGSize) intrinsicContentSize
{
CGSize s = [super intrinsicContentSize];
if ( self.numberOfLines == 0 )
{
// found out that sometimes intrinsicContentSize is 1pt too short!
s.height += 1;
}
return s;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment