kCTUnderlineStyleSingle不适用于某些角色

时间:2015-01-02 10:07:05

标签: ios iphone xcode6 nsattributedstring nsmutableattributedstring

我正在使用NSAttributedstring,其属性为kCTUnderlineStyleSingle。 性格正确地强调了。但是某些字符如 g,q,p 无法正确显示。

即使是用户也无法区分g和q。

有没有解决方案?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用此方法修复它:

- (void)addUnderlineWithRange:(NSRange)range
{
    int lineThickness = [EnvUtils isIpadEnv] ? 2 : 1;
    UIColor *lineColor = self.textColor;
    NSString *string = self.text;

    CGRect labelFrame = self.frame;
    CGSize expectedLabelSize = [NSString getMultilineTextSize:[string substringWithRange:range] forMaxSize:labelFrame.size font:self.font];
    CGSize expectedLabelFullSize = [NSString getMultilineTextSize:string forMaxSize:labelFrame.size font:self.font];

    CGSize xOffset = [NSString getMultilineTextSize:[string substringWithRange:NSMakeRange(0, range.location)] forMaxSize:labelFrame.size font:self.font];

    CGFloat originX = (labelFrame.size.width - expectedLabelFullSize.width) / 2 + xOffset.width;
    CGFloat originY = expectedLabelSize.height + (labelFrame.size.height - expectedLabelFullSize.height) / 2;
    CGRect lineFrame = CGRectMake(originX, originY, expectedLabelSize.width, lineThickness);

    UIView *lineView = [[[UIView alloc] initWithFrame:lineFrame] autorelease];
    lineView.backgroundColor = lineColor;

    [self.superview addSubview:lineView];
}