根据文档,CTFramesetterSuggestFrameSizeWithConstraints ()"确定字符串范围所需的帧大小".
不幸的是,这个函数返回的大小永远不准确.这是我在做的事情:
NSAttributedString *string = [[[NSAttributedString alloc] initWithString:@"lorem ipsum" attributes:nil] autorelease];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef) string);
CGSize textSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(rect.size.width, CGFLOAT_MAX), NULL);
Run Code Online (Sandbox Code Playgroud)
返回的大小始终具有正确的宽度计算,但高度始终略短于预期.
这是使用此方法的正确方法吗?
有没有其他方法来布局核心文本?
似乎我不是唯一遇到此方法问题的人.请参阅https://devforums.apple.com/message/181450.
编辑:我使用Quartz测量相同的字符串sizeWithFont:,为属性字符串和Quartz提供相同的字体.以下是我收到的测量数据:
核心文字:133.569336 x 16.592285
石英:135.000000 x 31.000000