看起来您没有设置 lineBreakMode。
我从 stackoverflow 获得了一些代码,然后将其放入一个类别中,您可以在这里了解它: http ://www.notthepainter.com/multi-line-uilabel/
但如果你只想要代码,这里是:
@interface UILabel (BPExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth;
@end
@implementation UILabel (EnkiExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth
{
if (fixedWidth < 0) {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);
} else {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
}
self.lineBreakMode = UILineBreakModeWordWrap;
self.numberOfLines = 0;
[self sizeToFit];
}
@end
你这样称呼它:
helpLabel_.text = @"You need to cut the blue wire and ground the red wire. Do not cut the red wire and ground the blue wire, that would be bad.";
[helpLabel_ sizeToFitFixedWidth: desiredWidth ];