I created a simple NSLayoutManager subclass that should allow me to change the first glyphs origin for a given range:
@implementation GRZCategoryLinkLayoutManager
- (void)invalidateLayoutForCharacterRange:(NSRange)charRange actualCharacterRange:(NSRangePointer)actualCharRange {
[super invalidateLayoutForCharacterRange:charRange actualCharacterRange:actualCharRange];
NSRange glyphRange = [self glyphRangeForCharacterRange:charRange actualCharacterRange:nil];
CGPoint firstGlyphLocation = [self locationForGlyphAtIndex:glyphRange.location];
firstGlyphLocation.x += 100;
NSLog(@"%@", NSStringFromCGPoint([self locationForGlyphAtIndex:glyphRange.location]));
// {100, 0}
[self setLocation:firstGlyphLocation forStartOfGlyphRange:glyphRange];
NSLog(@"%@", NSStringFromCGPoint([self locationForGlyphAtIndex:glyphRange.location]));
// {200, 0}
}
According to the log output, the location of the glyph is changed. But when the text ist displayed via an UITextView
, the glyph (and all the following glyphs) is drawn at {100, 0}
. The same happens if I draw the text in -drawRect:
- (void)drawRect:(CGRect)rect {
NSMutableAttributedString *string = [self.textStorage mutableCopy];
if (string && string.length > 0) {
CGContextRef context = UIGraphicsGetCurrentContext();
NSRange range = [self.layoutManager glyphRangeForTextContainer:self.textContainer];
[self.layoutManager drawGlyphsForGlyphRange:range atPoint:CGPointMake(0, 0)];
}
}
I used the code example from this question: NSLayoutManager: Calling setLocation(_:forStartOfGlyphRange:) disables kerning in the whole string?
Does anyone have an idea why the glyphs get drawn at the wrong origin?