Aks
Aks

Reputation: 33

NSAttributed string not working in swift

I am trying to use attributed string to customize a label but getting weird errors in swift.

func redBlackSubstring(substring: String) {
    self.font = UIFont(name: "HelveticaNeue", size: 12.0)
    var theRange: Range<String.Index>! = self.text?.rangeOfString(substring)
    var attributedString = NSMutableAttributedString(string: self.text!)

    let attribute = [NSForegroundColorAttributeName as NSString: UIColor.blackColor()]
    attributedString.setAttributes(attribute, range: self.text?.rangeOfString(substring))
    self.attributedText = attributedString
}

I have also tried using the below code

func redBlackSubstring(substring: String) {
    self.font = UIFont(name: "HelveticaNeue", size: 12.0)
    var theRange: Range<String.Index>! = self.text?.rangeOfString(substring)
    var attributedString = NSMutableAttributedString(string: self.text!)
    attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: self.text?.rangeOfString(substring))
    self.attributedText = attributedString
}

In both the cases, getting weird errors "Can not invoke 'setAttributes' with an argument list of type '([NSString : ..."

I have tried most of the solutions available on stack overflow and many other tutorials but, all of them resulting in such errors.

Upvotes: 3

Views: 3963

Answers (3)

Arvind Sharma
Arvind Sharma

Reputation: 56

The main culprit is Range. Use NSRange instead of Range. One more thing to note here is, simply converting self.text to NSString will give you error for forced unwrapping.

Thus, use "self.text! as NSString" instead.

func redBlackSubstring(substring: String) {
    self.font = UIFont(name: "HelveticaNeue", size: 12.0)!
    var range: NSRange = (self.text! as NSString).rangeOfString(substring)
    var attributedString = NSMutableAttributedString(string: self.text)
    attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: range)
    self.attributedText = attributedString
}

Upvotes: 4

diegomen
diegomen

Reputation: 1864

Try using NSRange instead of Range:

func redBlackSubstring(substring: String) {
    self.font = UIFont(name: "HelveticaNeue", size: 12.0)!
    var range: NSRange = (self.text as NSString).rangeOfString(substring)
    var attributedString = NSMutableAttributedString(string: self.text)
    attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: range)
    self.attributedText = attributedString
}

Upvotes: 0

Guillaume Algis
Guillaume Algis

Reputation: 11016

Your problem is that your passing a swift Range where a NSRange is expected.

The solution to get a valid NSRange from your string is to convert it to NSString first. See NSAttributedString takes an NSRange while I'm using a Swift String that uses Range.

So something like this should work:

let nsText = self.text as NSString
let theRange = nsText.rangeOfString(substring)  // this is a NSRange, not Range

// ... snip ...

attributedString.setAttributes(attribute, range: theRange)

Upvotes: 3

Related Questions