NSString drawInRect: withAttributes: неправильно отцентрировано при использовании NSKernAttributeName

Когда я используюdrawInRect:withAttributes: и передать как стиль абзаца сNSTextAlignmentCenter и ненулевое значение дляNSKernAttributeNameстрока не центрирована правильно. Я делаю что-то не так или это ожидаемое поведение? Есть ли обходной путь?

Скриншот:

Вы можете ясно видеть, что верхний текст не по центру.

Мой демонстрационный код:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIFont *font = [UIFont systemFontOfSize:15.0];
    [self drawString:@"88" inRect:rect font:font textColor:[UIColor blackColor]];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextAddRect(context, rect);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);
}

- (void)drawString:(NSString *)text
            inRect:(CGRect)contextRect
              font:(UIFont *)font
         textColor:(UIColor *)textColor
{
    CGFloat fontHeight = font.lineHeight;
    CGFloat yOffset = floorf((contextRect.size.height - fontHeight) / 2.0) + contextRect.origin.y;

    CGRect textRect = CGRectMake(contextRect.origin.x, yOffset, contextRect.size.width, fontHeight);

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByClipping;
    paragraphStyle.alignment = NSTextAlignmentCenter;

    [text drawInRect:textRect withAttributes:@{NSKernAttributeName: @(self.kerning),
                                            NSForegroundColorAttributeName: textColor,
                                            NSFontAttributeName: font,
                                            NSParagraphStyleAttributeName: paragraphStyle}];
}

Спасибо!

Обновить, благодаряэтот комментарий, Я добавилNSBackgroundColorAttributeName: [UIColor greenColor] к атрибутам и получил следующий результат:

Ответы на вопрос(1)

Ваш ответ на вопрос