Swift change color of text using NSMutableAttributedStrings
Asked Answered
P

3

6

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

I've tried this code, trying to translate from Obj-C but i cannot have it working

        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

The output of all this is enter image description here

where the number 34 correspond 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 I leave this piece of code regarding NSAttributedString the row text is displayed correctly.

Pollen answered 17/9, 2014 at 13:49 Comment(0)
K
11

I think the problem might lie in assigning to cell.textLabel?.text instead of cell.textLabel?.attributedText. Perhaps 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

Wasn't sure if you wanted the second part of the string to be red or another color so I made it black.

Kinsley answered 17/9, 2014 at 13:59 Comment(0)
B
1

If you can, avoid using NSMutableAttributedString and just pass in the attributes with the constructor:

private func PullToRefreshAttributedStringWithColor(color:UIColor) -> NSAttributedString {
  return NSAttributedString(string: "Pull to Refresh", attributes: [
      NSForegroundColorAttributeName:color
    ])
}
Boule answered 29/8, 2015 at 1:17 Comment(0)
L
0

Here is a example of attribute text 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
Laband answered 22/12, 2016 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.