2014-10-01 109 views
36

我试图强调一个字符串的一部分,例如,在“测试字符串”字符串“”的一部分。我正在使用NSMutableAttributedString,我的解决方案在iOS7上运行良好。下划线部分不工作

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

问题是我的解决方案不再工作在iOS8了。花了一个小时测试NSMutableAttributedString的多个变种后,我发现这个解决方案只在范围从0开始(长度可以不同)时才起作用。这是什么原因?我该如何解决这个问题?

+1

决心解决而不附加:http://stackoverflow.com/a/27106188/2833978 – NSSakly 2014-11-24 13:49:42

回答

85

更新: 通过考察这个问题:Displaying NSMutableAttributedString on iOS 8我终于找到了解决办法!

您应该在字符串的开头添加NSUnderlineStyleNone。

夫特4.0:

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

目的-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"]]; 

这样的方法的另一个好处是不存在任何范围。非常适合本地化的字符串。

好像它是苹果的bug :(

+0

我认为这是工作,但不幸的是我跑的应用程序在7.1模拟器......还没工作iOS8上。但避免范围是非常好的,谢谢! – KlimczakM 2014-10-01 10:51:27

+0

说实话,我没有在iOS 8上测试过我的代码。但是现在我已经完成了,终于找到了解决方案。 – kelin 2014-10-01 13:34:01

+0

工作正常!谢谢! – KlimczakM 2014-10-01 13:47:52

-2

添加颜色下划线属性:​ ​ ​

[attributedString addAttribute:NSUnderlineColorAttributeName 
        value:[UIColor redColor] 
        range:NSMakeRange(5, 6)]; 
5
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]; 

它为我

15

我发现,如果你申请UnderlineStyleNone整个字符串然后你可以有选择地应用下划线启动在部分中部:

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 
} 
+0

添加后如何删除下划线 – 2017-01-06 13:08:59

0

我以前在操场/模拟器以下扩展名(使用exidy的功能),它工作得很好,你可以改变/取决于添加属性您需要

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"])