NSAttributed string not working in swift

I am trying to use an attributed string to customize a shortcut, but I am getting strange 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 cases, getting strange errors "Cannot call" setAttributes "with an argument list like" ([NSString: ... "

I've tried most of the solutions available on Stack Overflow and many other tutorials, but they all lead to errors like this.

+3


source to share


3 answers


Range is the main culprit. Use NSRange instead of Range. One more thing to note here: simply converting self.text to NSString will give you an error to force unwrapping.

So, use "self.text!" Instead. like NSString.



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
}

      

+3


source


Your problem is that you are passing fast Range

where expected NSRange

.

The solution to get the correct string NSRange

from your string is to convert it to first NSString

. See NSAttributedString takes NSRange while I am using Swift String which 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)

      

+3


source


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
}

      

0


source







All Articles