为了账号安全,请及时绑定邮箱和手机立即绑定

在iOS 7中替换已弃用的-sizeWithFont:constrainedToSize:

在iOS 7中替换已弃用的-sizeWithFont:constrainedToSize:

iOS
慕哥6287543 2019-10-16 12:47:58
在iOS 7中,方法:- (CGSize)sizeWithFont:(UIFont *)font     constrainedToSize:(CGSize)size         lineBreakMode:(NSLineBreakMode)lineBreakMode 和方法:- (CGSize)sizeWithFont:(UIFont *)font不推荐使用。我该如何更换CGSize size = [string sizeWithFont:font                 constrainedToSize:constrainSize                     lineBreakMode:NSLineBreakByWordWrapping];和:CGSize size = [string sizeWithFont:font];
查看完整描述

3 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

您可以尝试以下方法:


CGRect textRect = [text boundingRectWithSize:size

                                 options:NSStringDrawingUsesLineFragmentOrigin

                              attributes:@{NSFontAttributeName:FONT}

                                 context:nil];


CGSize size = textRect.size;

只需将“ FONT”更改为“ [UIFont字体。...]”


查看完整回答
反对 回复 2019-10-16
?
炎炎设计

TA贡献1808条经验 获得超4个赞

这是简单的解决方案:


要求 :


CGSize maximumSize = CGSizeMake(widthHere, MAXFLOAT);

UIFont *font = [UIFont systemFontOfSize:sizeHere];

现在constrainedToSizeusage:lineBreakMode:在iOS 7.0中已弃用用法:


CGSize expectedSize = [stringHere sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];

现在,在更高版本的iOS 7.0中的用法将是:


// Let's make an NSAttributedString first

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:stringHere];

//Add LineBreakMode

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];

[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];

[attributedString setAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, attributedString.length)];

// Add Font

[attributedString setAttributes:@{NSFontAttributeName:font} range:NSMakeRange(0, attributedString.length)];


//Now let's make the Bounding Rect

CGSize expectedSize = [attributedString boundingRectWithSize:maximumSize options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;


查看完整回答
反对 回复 2019-10-16
  • 3 回答
  • 0 关注
  • 663 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信