2014-09-17 16 views
5

Ich habe eine UITableView und ich möchte den Text jeder Zeile mit verschiedenen Farben innerhalb der gleichen Zeile anzeigen.Swift Farbe des Textes ändern mit NSMutableAttributedStrings

Ich habe diesen Code versucht, versucht, sich von Obj-C zu übersetzen, aber ich kann es nicht

 let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject 

     var attrString: NSMutableAttributedString = NSMutableAttributedString(string: object.valueForKey("example1")!.description) 
     attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attrString.length)) 

     var stringToCell:String = String(format: "%@ %@", attrString, object.valueForKey("example2")!.description) 
     cell.textLabel?.text = stringToCell 

Der Ausgang des Ganzen ist enter image description here

, wo die Zahl 34 entsprechen object.valueForKey("example1")!.description haben Arbeits , so ist das Problem, dass die Nummer nicht rot ist, und der zweite Teil (object.valueForKey("example2")!.description) wird durch { ersetzt.

Wenn ich dieses Stück Code in Bezug auf NSAttributedString verlassen, wird der Zeilentext korrekt angezeigt.

Antwort

11

Ich denke, das Problem könnte in Zuordnung zu cell.textLabel?.text statt cell.textLabel?.attributedText liegen. Vielleicht so etwas wie dieses:

let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject 

var attrString: NSMutableAttributedString = NSMutableAttributedString(string: object.valueForKey("example1")!.description) 
attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attrString.length)) 

var descString: NSMutableAttributedString = NSMutableAttributedString(string: String(format: " %@", object.valueForKey("example2")!.description)) 
descString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSMakeRange(0, descString.length)) 

attrString.appendAttributedString(descString); 
cell.textLabel?.attributedText = attrString 

War mir nicht sicher, ob Sie den zweiten Teil des Strings, rot sein oder eine andere Farbe so gemacht wollte ich es schwarz.

1

Wenn Sie können, vermeiden NSMutableAttributedString verwenden und geben nur in den Attributen mit dem Konstruktor:

private func PullToRefreshAttributedStringWithColor(color:UIColor) -> NSAttributedString { 
    return NSAttributedString(string: "Pull to Refresh", attributes: [ 
     NSForegroundColorAttributeName:color 
    ]) 
} 
1

Hier ist ein Beispiel für Attributtext Swift 3

let mainText = "Here is a example of attributedString" 
let attributeText = "attributedString" 
let range = (mainText as NSString).range(of: attributeText) 
let attributedString = NSMutableAttributedString(string:mainText) 

attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: range) 
attribute.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14) , range: range) 

lblTitle.attributedText = attributedString 
Verwandte Themen