问题
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