sizeWithFont:ConstrainedToSize:lineBreakMode:方法在iOS 7中已弃用,我有点不确定如何处理这个问题。在互联网上进行了一些研究之后,我发现有一种处理这种方法的新方法,即:
[txt drawWithRect: options: attributes: context:]
这是我目前正在尝试运行的方法:
+ (CGSize)textSizeForText:(NSString *)txt
{
CGFloat width = [UIScreen mainScreen].applicationFrame.size.width * 0.75f;
CGFloat height = MAX([JSBubbleView numberOfLinesForMessage:txt],
[txt numberOfLines]) * [JSMessageInputView textViewLineHeight];
return [txt sizeWithFont:[JSBubbleView font]
constrainedToSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
lineBreakMode:NSLineBreakByWordWrapping];
}
但我很难将其转换为新方法。主要是使用lineBreakMode:在新方法中无处可去。有任何想法吗?
答案 0 :(得分:13)
在新方法中,对于换行符,您必须首先创建一个NSMutableParagraphStyle样式:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
然后通过传递所有必要的参数
来简单地使用新方法 CGRect textRect = [text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
context:nil];
return textRect.size;
如果你想要这个潮流,你可以做到
return ([text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
context:nil]).size;
希望有所帮助
我将在上面的答案中添加字体属性
return ([text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy, NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:14]}
context:nil]).size;