在iOS8中使用NSMutableAttributedStringstring的下划线不起作用

我尝试在一个string中加下划线,例如' test string 'string中的' string '部分。 我使用NSMutableAttributedString ,我的解决scheme在iOS7上运行良好。

 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"test string"]; [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(5, 6)]; myLabel.attributedText = attributedString; 

问题是我的解决scheme不再适用于iOS8 。 花了一个小时testingNSMutableAttributedString多个变种,我发现这个解决scheme只有当范围从0开始(长度可以不同)的作品。 这是什么原因? 我该如何解决这个问题?

更新:通过调查这个问题: 在iOS 8上显示NSMutableAttributedString我终于find了解决scheme!

你应该在string的开头添加NSUnderlineStyleNone。

Swift 3:

 let attributedString = NSMutableAttributedString() attributedString.append(NSAttributedString(string: "test ", attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue])) attributedString.append(NSAttributedString(string: "s", attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])) attributedString.append(NSAttributedString(string: "tring", attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleNone.rawValue])) 

Objective-C的:

  NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"test " attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"s" attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle), NSBackgroundColorAttributeName: [UIColor clearColor]}]]; [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"tring"]]; 

这种方法的另一个好处是没有任何范围。 非常好的本地化string。

似乎是苹果的错误:(

我发现,如果将UnderlineStyleNone应用于整个string,则可以select将下划线应用于从中间开始的部分:

 func underlinedString(string: NSString, term: NSString) -> NSAttributedString { let output = NSMutableAttributedString(string: string) let underlineRange = string.rangeOfString(term) output.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleNone.rawValue, range: NSMakeRange(0, string.length)) output.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: underlineRange) return output } 
 NSMutableAttributedString *signUpString = [[NSMutableAttributedString alloc] initWithString:@"Not a member yet?Sign Up now"]; [signUpString appendAttributedString:[[NSAttributedString alloc] initWithString:@" " attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}]]; [signUpString addAttributes: @{NSForegroundColorAttributeName:UIColorFromRGB(0x43484B),NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(17,11)]; signUpLbl.attributedText = [signUpString copy]; 

它为我工作

我在操场/模拟器中使用了以下扩展(使用exidy的function),它工作正常,您可以根据您的需要更改/添加属性

  extension NSMutableAttributedString { func changeWordsColour(terms:[NSString]) { let string = self.string as NSString self.addAttribute(NSForegroundColorAttributeName, value: UIColor.brownColor(), range: NSMakeRange(0, self.length)) for term in terms { let underlineRange = string.rangeOfString(term as String) self.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: underlineRange) } } } let myStr = NSMutableAttributedString(string: "Change Words Colour") myStr.changeWordsColour(["change","Colour"]) 

为下划线属性添加颜色:

 [attributedString addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 6)];