不能在 WKInterfaceLabel 上使用 NSAttributedString

Cannot use NSAttributedString on WKInterfaceLabel

我想将 WKInterfaceLabel 中的某些字符显示为下划线。

当范围位置为 0(= 字符串的开头)时,无论长度如何,它都能正常工作 - 范围内的字符带有下划线。

但是将位置设置为 0 以外的任何其他值都不起作用 - 没有下划线。

NSMutableAttributedString *attrString=[[NSMutableAttributedString alloc] initWithString:@"123.45"];
NSInteger attrStringLength =[attrString length];

NSRange range = NSMakeRange(0, 1);                // works
NSRange range = NSMakeRange(0, 2);                // works
NSRange range = NSMakeRange(0, attrStringLength); // works
NSRange range = NSMakeRange(1, 1); // does not work
NSRange range = NSMakeRange(2, 2); // does not work

[attrString addAttributes:@{
                            NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid) // style
                            }
                    range:range];

[self.valueLabel setAttributedText:attrString];

还有其他人有这个问题吗?我正在使用 Xcode 6.2 iOS 8.2 并在模拟器中进行测试。

它与您提供的相同配置完美配合。

在动态字符串分配的情况下,可能存在位置和长度问题,您可能没有获得足够的文本长度,而您正试图添加下划线。