NSLayoutManager: Calling setLocation(_:forStartOfGlyphRange:) disables kerning in the whole string?

折月煮酒 提交于 2019-12-21 23:18:13

问题


I created simple NSLayoutManager subclass that allows to set custom layout location for defined substrings:

class PositionableLayoutManager: NSLayoutManager {
    var xOffsetsPerGlyphRange = [(NSRange, CGPoint)]()

    override func invalidateLayoutForCharacterRange(charRange: NSRange, actualCharacterRange actualCharRange: NSRangePointer) {
        super.invalidateLayoutForCharacterRange(charRange, actualCharacterRange: actualCharRange)
        for (range, offset) in xOffsetsPerGlyphRange {
            setLocation(offset, forStartOfGlyphRange: range)
        }
    }
}

Now, I can define custom location of any character range like this:

let glyphRange = layoutManager.glyphRangeForCharacterRange(charRange, actualCharacterRange: nil)
layoutManager.xOffsetsPerGlyphRange.append((glyphRange, customPointLocation))
layoutManager.invalidateLayoutForCharacterRange(NSRange(location: 0, length: textStorage.length), actualCharacterRange: nil)

It works fine, the only problem is that any glyph range repositioned in this way is laid out and drawn without kerning. Here is an example:

In green box there is text drawn using my PositionableLayoutManager with without kerning, in pink box there is same text with kering drawn using UILabel for comparison.

How can I do the same thing but with proper kerning?

来源:https://stackoverflow.com/questions/31480931/nslayoutmanager-calling-setlocation-forstartofglyphrange-disables-kerning-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!