In iOS 6, I am using :
CGSize labelSize = [self.text sizeWithFont:self.font constrainedToSize:size lineBreakMode:self.lineBreakMode];
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , labelSize.width, self.frame.size.height);
To dynamically resize a UILabel. This does not work in iOS 7 so I tried:
NSString *text = self.text;
CGFloat width = size.width;
UIFont *font = self.font;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
attributes:@{ NSFontAttributeName: font }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesDeviceMetrics
context:nil];
CGSize size = rect.size;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , size.width, self.frame.size.height);
This is inside a category on UILabel, but this is not working also... Any ideas what I should be using?
See Question&Answers more detail:os