Reputation: 361
I have a custom class of UIOutlineLabel which draws an outline around the text within a label. Since updating to Swift 4 I get the following error:
Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedStringKey : Any]?'.
I have tried changing the strokeTextAttributes to:
as! [NSAttributedStringKey : Any]
but this results in a runtime error.
There are also the Swift Language runtime warnings of 'UIOutlineLabel setOutlineWidth is deprecated and will be removed in Swift 4' & 'UIOutlineLabel setOutlineColor is deprecated and will be removed in Swift 4'.
Old Code:
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)
}
}
Upvotes: 2
Views: 4421
Reputation: 309
I think you should use:
override func drawText(in rect: CGRect) {
let strokeTextAtrributes: [NSAttributedStringKey : Any] = [
NSAttributedStringKey.strokeColor : outlineColor,
NSAttributedStringKey.strokeWidth : -1 * outlineWidth,
]
self.attributedText = NSAttributedString(string: self.text ?? "", attributes: strokeTextAttributes)
super.drawText(in: rect)
}
because attributes argument expects an [NSAttributedStringKey : Any]?
type
Upvotes: 2