Giving Framesetter the correct line spacing adjustment

后端 未结 2 1449
隐瞒了意图╮
隐瞒了意图╮ 2021-01-03 11:27

Several posts have noted difficulties with getting an exact height out of CTFramesetterSuggestFrameSizeWithConstraints, and here, (framesetter post), @Chris DeSalvo gives wh

2条回答
  •  一向
    一向 (楼主)
    2021-01-03 12:04

    Answers to the 3 questions I had above:

    1. Yes, “leading” really what is meant by kCTParagraphStyleSpecifierLineSpacingAdjustment. Or at any rate, it works as expected.
    2. Use CTFontGetLeading(fontRef) to get the font's normal leading, or plug in whatever value (as a CGFloat) you choose.
    3. N/A.

    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.

    There are two caveats:

    1. If you try to calculate heights incrementally, one string at a time, each string containing an initial line break, framesetter will consider that line break to represent an entire line, not just the leading. If you want the height of the concatenated strings, you have to feed that concatenation to the framesetter. Of course, you could keep track of the incremental height differences, but there's no way to avoid having framesetter recalculate the earlier string dimensions.
    2. CATextLayer ignores spacing adjustments (and other attributes). If framing per exact string height is an issue, you must draw direct to a CALayer.

    And there is one mystery: What is going on with UIFont's deprecated leading? Leading and lineHeight are two distinct things.

提交回复
热议问题