Ios 下划线故障

Ios 下划线故障,ios,uilabel,nsattributedstring,core-text,uifont,Ios,Uilabel,Nsattributedstring,Core Text,Uifont,我正在尝试使用自定义字体(Helvetica粗体)为属性文本添加下划线。然而,模拟器和设备上的突出故障。像这样: 这是一个UILabel,但我也可以在UITextView中重现这个问题。在某些行上,文本渲染器似乎错误计算了在何处绘制下划线 赤裸裸的代码: - (void)viewDidLoad { [super viewDidLoad]; UIFont *font = [UIFont fontWithName:@"HelveticaRounded-Bold" size:18]; s

我正在尝试使用自定义字体(Helvetica粗体)为属性文本添加下划线。然而,模拟器和设备上的突出故障。像这样:

这是一个
UILabel
,但我也可以在
UITextView
中重现这个问题。在某些行上,文本渲染器似乎错误计算了在何处绘制下划线

赤裸裸的代码:

- (void)viewDidLoad {
  [super viewDidLoad];
  UIFont *font = [UIFont fontWithName:@"HelveticaRounded-Bold" size:18];
  self.label.font = font;
  self.label.numberOfLines = 0;
  NSDictionary *attributes = @{ NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) };
  self.label.attributedText = [[NSAttributedString alloc] initWithString:@"This is a test. And the test has failed. What did I do to deserve this? Why NSUnderlineStyleAttributeName? Why?"
                                                              attributes:attributes];
}
有趣的事实:下划线在24码或以上时不会出现问题(!)。它也不会使用
TTTAttributedLabel
重新编程


知道这里发生了什么吗?

使用
NSUnderlineStyleThick
时,非视网膜设备似乎出现了故障。切换到
NSUnderlineStyleSingle
似乎可以解决这个问题。因此,如果要在其他设备上保留
NSUnderlineStyleSingle
,您可以为非视网膜设备设置使用
NSUnderlineStyleThick
的条件


编辑:刚刚意识到您在示例代码中使用了
NSUnderlineStyleSingle
:在我的代码中,这适用于我给定的字体/大小,这只是系统字体。这解决了我这边的问题。因此,似乎这个修复可能不是通用的,但它在我的情况下起了作用。

对于非视网膜显示器上的UIButton,我也有同样的问题。我只是将非视网膜设备的“NSUnderlineStyleSingle”改为“NSUnderlineStyleDouble”

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleString?titleString:@"" attributes:@{ NSFontAttributeName: button.titleLabel.font}];

[buttonTitleString addAttribute:NSUnderlineStyleAttributeName value:[CommonUtil getValueForUnderlineStyle] range:NSMakeRange(0, [buttonTitleString length])];
[button.titleLabel setAttributedText:attributedString];

+ (NSNumber *)getValueForUnderlineStyle {
NSNumber *numberToReturn = @(NSUnderlineStyleSingle);
if(!(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && [[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [UIScreen mainScreen].scale > 1)) {
    // Non Retina iPad
    numberToReturn = @(NSUnderlineStyleDouble);
}
return numberToReturn;
}

我也看到了这个问题。你找到解决办法了吗?我试过一种字体,我知道它在苹果的文本编辑中有问题,问题也出现了。我想知道是不是字体不好?