本文介绍了内容弃用的sizeWithFont:constrainedToSize到boundingRectWithSize:options:attributes:context:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何转换
CGSize labelHeighSize = [text sizeWithFont: [UIFont systemFontOfSize:16] constrainedToSize:maximumSize lineBreakMode:NSLineBreakByTruncatingTail];
到
CGSize labelHeighSize = [text boundingRectWithSize:maximumSize options: attributes: context:
推荐答案
首先是方法:
- (CGRect) boundingRectWithSize:(CGSize)size
options:(NSStringDrawingOptions)options
attributes:(NSDictionary *)attributes
context:(NSStringDrawingContext *)context;
返回 CGRect
而不是 CGSize
所以你需要使用 CGRect
。
returns CGRect
not the CGSize
so you need to use CGRect
.
编辑
根据apple docs,参见,它说
以下是您可以使用的示例代码:
Below is the sample code that you can use:
NSString *text = @"Some text to measure";
UIFont *labelFont = [UIFont systemFontOfSize:16];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
//set the line break mode
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:labelFont,
NSFontAttributeName,
paragraphStyle,
NSParagraphStyleAttributeName,
nil];
//assume your maximumSize contains {255, MAXFLOAT}
CGRect lblRect = [text boundingRectWithSize:(CGSize){225, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attrDict
context:nil];
CGSize labelHeighSize = lblRect.size;
这篇关于内容弃用的sizeWithFont:constrainedToSize到boundingRectWithSize:options:attributes:context:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!