答案 0 :(得分:0)
@implementation UILabel (dynamicSizeWidth)
- (void)resizeToStretch {
float width = [self expectedWidth];
CGRect newFrame = [self frame];
newFrame.size.width = width;
[self setFrame:newFrame];
}
- (float)expectedWidth{
[self setNumberOfLines:1];
CGSize maximumLabelSize = CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX);
CGSize expectedLabelSize = [[self text] sizeWithFont:[self font]
constrainedToSize:maximumLabelSize
lineBreakMode:[self lineBreakMode]];
return expectedLabelSize.width;
}
@end
除了iOS6,你可以使用Autolayout,将行数设置为0,框架将适当调整你的标签大小以适合你的文字。