2014-10-01 53 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"])