NSString drawInRect:withAttributes:使用NSKernAttributeName时未正确居中 [英] NSString drawInRect:withAttributes: not correctly centered when using NSKernAttributeName
问题描述
当我使用 drawInRect:withAttributes:
并传入带有 NSTextAlignmentCenter
的段落样式且非零 NSKernAttributeName
的值,字符串无法正确居中。我做错了什么或这是预期的行为?有解决方法吗?
When I use drawInRect:withAttributes:
and pass in both a paragraph style with NSTextAlignmentCenter
and a non-zero value for NSKernAttributeName
, the string does not get centered correctly. Am I doing something wrong or is this expected behavior? Is there a workaround?
屏幕截图:
您可以清楚地看到顶部文字未正确居中。
You can clearly see that top text is not centered correctly.
我的演示代码:
- (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]
并获得了以下结果:
Update, thanks to this comment, I added NSBackgroundColorAttributeName: [UIColor greenColor]
to the attributes and got the following result:
推荐答案
字距调整必须仅应用于每个字距调整对的第一个字符。
如果要在所有 n
字符之间显示字符串,则必须为第一个设置字距调整
字符。
属性n-1
The kerning must be applied only to the first character of each kerning pair.
If you want to display a string with kerning between all n
characters then the kerning
attribute must be set for the first n-1
characters.
而不是:
[text drawInRect:textRect withAttributes:@{NSKernAttributeName: @(self.kerning),
NSForegroundColorAttributeName: textColor,
NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle}];
您必须创建一个属性字符串,以便您可以设置字距调整属性
表示特定范围而不是整个字符串:
you have to create an attributed string so that you can set the kerning attribute for a specific range instead of the entire string:
NSMutableAttributedString *as = [[NSMutableAttributedString alloc]
initWithString:text
attributes:@{
NSForegroundColorAttributeName: textColor,
NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle}];
[as addAttribute:NSKernAttributeName
value:@(self.kerning)
range:NSMakeRange(0, [text length] - 1)];
[as drawInRect:textRect];
这里是字符串1234和字距-4.0的结果:
Here the result for the string "1234" and kerning -4.0:
这篇关于NSString drawInRect:withAttributes:使用NSKernAttributeName时未正确居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!