gpt4 book ai didi

ios - Swift 4 转换错误 - 类型 'NSAttributedStringKey' 没有成员 'foregroundColorNSAttributedStringKey'

转载 作者:搜寻专家 更新时间:2023-11-01 07:04:41 25 4
gpt4 key购买 nike

我将我的代码从 Swift 3 转换为 Swift 4 但出现此错误:

Type 'NSAttributedStringKey' has no member '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)

关于ios - Swift 4 转换错误 - 类型 'NSAttributedStringKey' 没有成员 'foregroundColorNSAttributedStringKey',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48762758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com