我正在尝试调整 UILabel 的大小以适应 NSString 的范围,但我在 iOS 7.0.3 和 iOS 7.1.1 中看到了不同的结果。正如您在下面看到的,iOS 7.0.3 似乎无法正确绘制文本。
示例 1:文本被绘制到标签的底部,几乎在边界之外:
示例 2:文本绘制在 1 行(而不是 2 行)上,并且没有发生自动换行,只有尾部截断。
这是我用于上面列出的两个 iOS 版本的代码。
CGSize boundingSize = CGSizeMake(214, 9999);
CGRect boundingRect = CGRectZero;
[self.nameLabel setNumberOfLines:2];
// for iOS7
if([self.place.placeName respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]){
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
boundingRect = [self.place.placeName boundingRectWithSize:boundingSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
self.nameLabel.font, NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName, nil]
context:nil];
}else{
// pre iOS7
CGSize size = [self.place.placeName sizeWithFont:self.nameLabel.font constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping];
boundingRect = CGRectMake(0, 0, size.width, size.height);
}
[self.nameLabel setFrame:CGRectMake(CGRectGetMaxX(self.photoImageView.frame)+15,
CGRectGetMinY(self.photoImageView.frame),
boundingRect.size.width, boundingRect.size.height)];
[self.nameLabel setText:[place placeName]];
有任何想法吗?提前致谢。