[英]sizeWithFont: ConstrainedToSize: lineBreakMode: method is deprecated in iOS 7
sizeWithFont: ConstrainedToSize: lineBreakMode: method is deprecated in iOS 7 and I'm a little unsure how to handle this exactly. sizeWithFont:ConstrainedToSize:lineBreakMode:方法在iOS 7中已弃用,我有点不确定如何处理这个问题。 After a little of research on the internet I found that there's a new method for handling this, which is: 经过对互联网的一些研究后,我发现有一种新方法可以解决这个问题,即:
[txt drawWithRect: options: attributes: context:]
This is the method that I am currently attempting to run: 这是我目前正在尝试运行的方法:
+ (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];
}
But I'm having a hard time converting it to the new method. 但我很难将其转换为新方法。 Mainly with the lineBreakMode: which is nowhere in the new method. 主要是使用lineBreakMode:在新方法中无处可去。 Any ideas? 有任何想法吗?
in the new method, for line break, you have to create a NSMutableParagraphStyle Style first: 在新方法中,对于换行符,您必须首先创建一个NSMutableParagraphStyle样式:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
Then simply use the new method by passing all the necessary parameters 然后通过传递所有必要的参数来简单地使用新方法
CGRect textRect = [text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
context:nil];
return textRect.size;
if you want this to be tide, you can do 如果你想要这个潮流,你可以做到
return ([text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
context:nil]).size;
Hope that helps 希望有所帮助
I will add the font attributes in above answer 我将在上面的答案中添加字体属性
return ([text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy, NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:14]}
context:nil]).size;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.