2016-07-20 9 views
2

Ich verwende den folgenden Code, um die foreground color und font einer Attributzeichenfolge mithilfe von zu ändern. Aber die Schrift Wechsel perfekt ohne Probleme, aber die foreground color ändert sich nichtZugewiesene Zeichenfolge Die Vordergrundfarbe funktioniert nicht in Swift

var checklistText = NSMutableAttributedString() 
     checklistText = NSMutableAttributedString(string: "\(lblChecklistName.text!),\(checklistName)") 
     checklistText.addAttribute(NSFontAttributeName, 
            value: UIFont(
            name: "Helvetica", 
            size: 11.0)!, 
            range: NSRange(location: lblChecklistName.text!.length(), length: checklistName.length())) 
     checklistText.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location: lblChecklistName.text!.length(), length: checklistName.length()+1)) 
     lblChecklistName.attributedText = checklistText 
+0

ich denke, String doesn‘ t nicht Methode 'length', verwende ich' .characters.count' –

+0

Es gibt einige p Problem mit Reichweite, die Sie passieren. –

+0

Wenn Sie korrekte Bereiche verwenden, funktioniert Ihr Code einwandfrei. Überprüfen Sie die Bereiche, die Sie verwenden. – Moritz

Antwort

0

Referenzform: NSMutableAttributedString

var myString:NSString = "I AM KIRIT MODI" 
var myMutableString = NSMutableAttributedString() 
myMutableString = NSMutableAttributedString(string: myString as 
String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!]) 
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!]) 
      myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:2,length:2)) 
lbl_First.attributedText = myMutableString 

See Bild:

enter image description here

+0

Danke für Ihre Antwort, ich habe das versucht, aber das funktioniert nicht –