CTFramesetterSuggestFrameSizeWithConstraints有时返回不正确的大小? [英] CTFramesetterSuggestFrameSizeWithConstraints sometimes returns incorrect size?

查看:1240
本文介绍了CTFramesetterSuggestFrameSizeWithConstraints有时返回不正确的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中, CTFramesetterSuggestFrameSizeWithConstraints 有时会返回 CGSize ,其高度不足以包含所有正在传递给它的文本。我确实看了这个答案。但在我的情况下,文本框的宽度需要是恒定的。有没有其他/更好的方法来确定我的属性字符串的正确高度?谢谢!

In the code below, CTFramesetterSuggestFrameSizeWithConstraints sometimes returns a CGSize with a height that is not big enough to contain all the text that is being passed into it. I did look at this answer. But in my case the width of the text box needs to be constant. Is there any other/better way to figure out the correct height for my attributed string? Thanks!

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedString);
CGSize tmpSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), NULL, CGSizeMake(self.view.bounds.size.width, CGFLOAT_MAX), NULL); 
CGSize textBoxSize = CGSizeMake((int)tmpSize.width + 1, (int)tmpSize.height + 1);


推荐答案

CTFramesetterSuggestFrameSizeWithConstraints()坏了。我一会儿就提出了一个错误。您可以选择使用 CTFramesetterCreateFrame(),其路径足够高。然后你可以测量你得到的CTFrame的矩形。请注意,您不能使用 CGFLOAT_MAX 作为高度,因为CoreText使用iPhone的翻转坐标系,并将其文本定位在框的顶部。这意味着如果你使用 CGFLOAT_MAX ,你将没有足够的精确度来实际告诉盒子的高度。我建议使用像10,000这样的高度,因为它比屏幕本身高10倍,但却为生成的矩形提供了足够的精度。如果你需要布置更高版本的文本,你可以为文本的每个部分多次执行此操作(您可以向CTFrameRef询问其能够布局的原始字符串中的范围)。

CTFramesetterSuggestFrameSizeWithConstraints() is broken. I filed a bug on this a while back. Your alternative is to use CTFramesetterCreateFrame() with a path that is sufficiently high. Then you can measure the rect of the CTFrame that you get back. Note that you cannot use CGFLOAT_MAX for the height, as CoreText uses a flipped coordinate system from the iPhone and will locate its text at the "top" of the box. This means that if you use CGFLOAT_MAX, you won't have enough precision to actually tell the height of the box. I recommend using something like 10,000 as your height, as that's 10x taller than the screen itself and yet gives enough precision for the resulting rectangle. If you need to lay out even taller text, you can do this multiple times for each section of text (you can ask CTFrameRef for the range in the original string that it was able to lay out).

这篇关于CTFramesetterSuggestFrameSizeWithConstraints有时返回不正确的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆