Quickly change text color using NSMutableAttributedStrings

I have a UITableView , and I would like to display the text of each row using different colors within the same row.

I tried this code trying to translate from Obj-C, but I can't work it

  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 

Conclusion of all this enter image description here

where the number 34 corresponds to object.valueForKey("example1")!.description , so the problem is that the number is not red, and the second part ( object.valueForKey("example2")!.description ) is replaced by { .

If you leave this piece of code in relation to NSAttributedString , the text of the line will be displayed correctly.

+5
source share
3 answers

I think the problem might be in assigning cell.textLabel?.text cell.textLabel?.attributedText instead of cell.textLabel?.attributedText . Maybe something like this:

 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 

Not sure if you want the second part of the line to be red or another color, so I made it black.

+11
source

If you can, do not use NSMutableAttributedString and just pass attributes using the constructor:

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

Here is an example of Swift 3 attribute text

 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 
+1
source

Source: https://habr.com/ru/post/1202771/


All Articles