1

我正在使用NSAttributedstring属性kCTUnderlineStyleSingle。字符下划线正确。但是像g、q、p这样的字符是不可见的。

甚至用户也无法区分 g 和 q。

有什么解决办法吗?

在此处输入图像描述

4

1 回答 1

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];
}
于 2016-04-27T12:49:37.400 回答