2

Can anyone see what is wrong in the following Swift code?

func myFunction(color:UIColor) {// Changes the Shadow color on textLabel
    var attributes = textLabel.attributedText?.attributesAtIndex(0, effectiveRange: nil)
    let shadow = attributes![NSShadowAttributeName] as! NSShadow
    shadow.shadowColor = color

    let mutabAttrString = NSMutableAttributedString(attributedString: textLabel.attributedText!)
    mutabAttrString.removeAttribute(NSShadowAttributeName, range: NSMakeRange(0,mutabAttrString.length))
    mutabAttrString.addAttribute(NSShadowAttributeName, value: shadow, range: NSMakeRange(0,mutabAttrString.length))
    textLabel.attributedText = NSAttributedString(attributedString: mutabAttrString)
}

I use similar code to change the NSForegroundColorAttributeName and NSStrokeColorAttributeName (it works) but for some reason here it does not work with NSShadowAttributeName. I know it is slightly different, but what am I missing?

Michel
  • 10,303
  • 17
  • 82
  • 179
  • You shouldn't have to call the `mutabAttrString.removeAttribute(NSShadowAttributeName, ...` since you replace it afterwards. – Larme Nov 19 '15 at 10:07
  • I see, so adding replaces in case there is already something there. If it's a dictionary you are right. Thanks for the tip, good to know. But that does not explain why the code is not working. – Michel Nov 20 '15 at 01:01

0 Answers0