问题描述
有几篇帖子指出从CTFramesetterSuggestFrameSizeWithConstraints中获取精确高度的困难,在这里,,@ Chris DeSalvo给出了看似最终修正的内容:使用正确的行间距调整添加段落样式设置。
Several posts have noted difficulties with getting an exact height out of CTFramesetterSuggestFrameSizeWithConstraints, and here, (framesetter post), @Chris DeSalvo gives what looks like the definitive fix: add a paragraph style setting with the correct line spacing adjustment.
DeSalvo获得领先 通过从其lineHeight移除UIFont的上升器和下降器。我想知道如何与 CTFontGetLeading
进行比较。
DeSalvo gets his "leading" by removing UIFont’s ascender and descender from its lineHeight. I wondered how that would compare to CTFontGetLeading
.
我使用这样创建的字体:
I worked with fonts created like this:
CTFontRef fontr = CTFontCreateWithName((CFStringRef)@"Helvetica Neue", 16.0f, NULL);
UIFont *font = [UIFont fontWithName:@"Helvetica Neue" size:16.0f];
价格差别很大:
- 0.448 CTFontGetLeading
- 2.360 DeSalvo的公式:UIFont lineHeight - ascender + descender
以下是其他一些UIFont值:
Here are some other UIFont values:
- 21.000 UIFont的lineHeight
- 15.232 UIFont的ascender(来自基线的Y坐标)
- -3.408 UIFont的下行(从基线开始的Y坐标)
- 08.368 UIFont的xHeight
- 21.000 UIFont’s lineHeight
- 15.232 UIFont’s ascender (Y coord from baseline)
- -3.408 UIFont’s descender (Y coord from baseline)
- 08.368 UIFont’s xHeight
以下是Ken Thomases询问的CTFont值:
And here are the CTFont values that Ken Thomases inquired about:
- 11.568001 CTFontGetCapHeight
- 08.368 CTFontGetXHeight
- -15.216001,-7.696001,38.352001,24.928001 CTFontGetBoundingBox
- 15.232 CTFontGetAscent
- 03.408 CTFontGetDescent(类ref表示根据字体参考的点大小和矩阵缩放的缩放字体 - 下降度量 - 这显然意味着它是绝对值从基线开始的Y坐标值是多少?)
- 11.568001 CTFontGetCapHeight
- 08.368 CTFontGetXHeight
- -15.216001, -7.696001, 38.352001, 24.928001 CTFontGetBoundingBox
- 15.232 CTFontGetAscent
- 03.408 CTFontGetDescent (class ref says "scaled font-descent metric scaled according to the point size and matrix of the font reference" -- which apparently means that it is the absolute value of the Y coordinate from the baseline?)
我注意到UIFont之前有一个专门用于领先的属性,但它已被弃用,我们建议使用 lineHeight
。因此,对于相同的字体,UIFont认为导致 21 和CTFontRef .448 ?有些事情不对。
I note that UIFont previously had a property specifically for "leading," but it has been deprecated and we are advised to use lineHeight
instead. So UIFont considers leading to be 21 and CTFontRef .448 for the same font? Something’s not right.
三个问题:
- 领先真的是什么是指kCTParagraphStyleSpecifierLineSpacingAdjustment?
- 如果是这样,我应该使用哪种方法/公式来获取它?
- 如果没有,我该怎么用行距调整?
推荐答案
我上面提到的3个问题的答案:
Answers to the 3 questions I had above:
- 是的,领先真正意味着什么是kCTParagraphStyleSpecifierLineSpacingAdjustment。或者无论如何,它按预期工作。
- 使用
CTFontGetLeading(fontRef)
来获得字体的正常引导,或插入任何内容您选择的价值(作为CGFloat)。 - N / A.
- Yes, "leading" really what is meant by kCTParagraphStyleSpecifierLineSpacingAdjustment. Or at any rate, it works as expected.
- Use
CTFontGetLeading(fontRef)
to get the font's normal leading, or plug in whatever value (as a CGFloat) you choose. - N/A.
答案1和2工作:在属性字符串的paragraphStyle属性中指定一个前导值将使Core-Text框架集能够计算它的高度完全。
Answers 1 and 2 work: Specifying a leading value in a paragraphStyle attribute of your attributed string will enable the Core-Text framesetter to calculate its height exactly.
有两点需要注意:
- 如果您尝试逐步计算高度,一次一个字符串,每个字符串包含一个初始换行符,framesetter会将该换行符视为整行,而不仅仅是前导。如果需要连接字符串的高度,则必须将该连接提供给框架设置器。当然,您可以跟踪增量高度差异,但是没有办法避免让framesetter重新计算早期的字符串维度。
- CATextLayer忽略间距调整(和其他属性)。如果确定每个精确的弦高是一个问题,你必须直接绘制到CALayer。
还有一个谜:什么是在UIFont被弃用的领先?领先和lineHeight是两个不同的东西。
And there is one mystery: What is going on with UIFont's deprecated leading? Leading and lineHeight are two distinct things.
这篇关于为Framesetter提供正确的行间距调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!