【发布时间】:2017-09-22 08:03:28
【问题描述】:
我有一个 UIOutlineLabel 的自定义类,它在标签内的文本周围绘制轮廓。由于更新到 Swift 4,我收到以下错误:
无法将类型“[String:Any]”的值转换为预期的参数类型“[NSAttributedStringKey:Any]?”。
我已尝试将 strokeTextAttributes 更改为:
as! [NSAttributedStringKey : Any]
但这会导致运行时错误。
还有“UIOutlineLabel setOutlineWidth 已弃用并将在 Swift 4 中删除”和“UIOutlineLabel setOutlineColor 已弃用并将在 Swift 4 中删除”的 Swift 语言运行时警告。
旧代码:
import UIKit
class UIOutlineLabel: UILabel {
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
var outlineWidth: CGFloat = 1
var outlineColor: UIColor = UIColor.white
override func drawText(in rect: CGRect) {
let strokeTextAttributes = [
NSAttributedStringKey.strokeColor.rawValue : outlineColor,
NSAttributedStringKey.strokeWidth : -1 * outlineWidth,
] as! [String : Any]
self.attributedText = NSAttributedString(string: self.text ?? "", attributes: strokeTextAttributes)
super.drawText(in: rect)
}
}
【问题讨论】: