2014-10-01 14 views
36

staram się podkreślić część napisu, na przykład, „ciąg” w „część testu ciąg” string. Używam NSMutableAttributedString i moje rozwiązanie działało dobrze na iOS7.Podkreślenie częścią łańcucha przy użyciu NSMutableAttributedString w systemów iOS 8 nie działa

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

Problemem jest to, że moje rozwiązanie nie działa w iOS8 więcej. Po spędzeniu godziny na testowaniu wielu wariantów NSMutableAttributedString, dowiedziałem się, że to rozwiązanie działa tylko wtedy, gdy zakres zaczyna się od 0 (długość może się różnić). Jaki jest tego powód? Jak mogę to obejść?

+1

rozwiązanie resolve bez dołączania: http://stackoverflow.com/a/27106188/2833978 – NSSakly

Odpowiedz

85

Aktualizacja: Badając to pytanie: Displaying NSMutableAttributedString on iOS 8 końcu znalazłem rozwiązanie!

Powinieneś dodać NSUnderlineStyleNone na początku łańcucha.

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

Cel 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"]]; 

Innym Bonus takiego rozwiązania jest brak jakichkolwiek zakresach. Bardzo miły dla zlokalizowanych ciągów.

Wydaje się jakby to jest jabłko bug :(

+0

myślałem, że to działa, ale niestety wpadłem aplikację w symulatorze 7.1 ... Nadal nie działa na systemów iOS 8. Ale unikanie zasięgu jest bardzo miłe, dziękuję! – KlimczakM

+0

Szczerze mówiąc, nie przetestowałem mojego kodu na iOS 8. Ale teraz zrobiłem i wreszcie znalazłem rozwiązanie. – kelin

+0

To zadziałało! Dzięki! – KlimczakM

-2

Dodaj kolor do atrybutu podkreślenia: ​ ​ ​

[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]; 

on pracował dla mnie

15

stwierdziliśmy, że jeśli stosuje UnderlineStyleNone do całego łańcucha można następnie selektywnie zastosować podkreślenie do części, która rozpoczyna się w środkowa:

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

jak usunąć podkreślenie po dodaniu –

0

użyłem następujące rozszerzenia (za pomocą funkcji exidy'S) na placu zabaw/symulator i to działało w porządku, można zmienić/dodać atrybuty w zależności od Państwa potrzebuje

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"]) 
Powiązane problemy