gpt4 book ai didi

ios - 内容已弃用 sizeWithFont :constrainedToSize to boundingRectWithSize:options:attributes:context:

转载 作者:行者123 更新时间:2023-11-28 22:10:22 27 4
gpt4 key购买 nike

我怎样才能转换

 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

编辑根据苹果文档,请参阅 here , 它说

This option is ignored if NSStringDrawingUsesLineFragmentOrigin is not also set. In addition, the line break mode must be either NSLineBreakByWordWrapping or NSLineBreakByCharWrapping for this option to take effect. The line break mode can be specified in a paragraph style passed in the attributes dictionary argument of the drawing methods.

下面是您可以使用的示例代码:

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;

关于ios - 内容已弃用 sizeWithFont :constrainedToSize to boundingRectWithSize:options:attributes:context:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23075693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com