我将代码从Swift 3转换为Swift 4,但收到此错误:
类型“NSAttributedStringKey”没有成员“foregroundColorNSAttributedStringKey”
我的代码是:
let labelText = NSMutableAttributedString(string: (self.productDetailsInfo?.productAttributes?[indexPath.row].Name as String?)!)
labelText.append(NSAttributedString(string:"*"))
let selectedRange = NSMakeRange(labelText.length - 1, 1);
labelText.addAttribute(NSAttributedStringKey.foregroundColorNSAttributedStringKey.foregroundColor, value: UIColor.red, range: selectedRange)
labelText.addAttribute(NSAttributedStringKey.baselineOffset, value: 2, range: selectedRange)
最佳答案
更换线
labelText.addAttribute(NSAttributedStringKey.foregroundColorNSAttributedStringKey.foregroundColor, value: UIColor.red, range: selectedRange)
与
labelText.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: selectedRange)
您也可以使用
addAttributes
方法为一个范围一次设置多个属性 labelText.addAttributes([NSAttributedStringKey.foregroundColor:UIColor.red,NSAttributedStringKey.backgroundColor:UIColor.blue], range: selectedRange)