Faw*_*med 20 nsattributedstring swift swift4 nsattributedstringkey
如何将类型的值转换'[String : AnyObject]?'为预期的参数类型'[NSAttributedStringKey : Any]?'?
open class func drawText(context: CGContext, text: String, point: CGPoint,
align: NSTextAlignment, attributes: [String : AnyObject]?)
{
var point = point
if align == .center
{
point.x -= text.size(withAttributes: attributes).width / 2.0
}
else if align == .right
{
point.x -= text.size(withAttributes: attributes).width
}
NSUIGraphicsPushContext(context)
(text as NSString).draw(at: point, withAttributes: attributes)
NSUIGraphicsPopContext()
}
Run Code Online (Sandbox Code Playgroud)
Cha*_*tka 28
这是Swift 4的一个新特性.所有采用字符串标识符和/或字典键的Cocoa方法现在都有自己的键类型.这样做的原因是在旧方案中添加了一些类型的安全性,有可能意外地通过一个String意图传递一个常量,这意味着与其他一些API一起使用,但现在在Swift 4中,这将导致编译错误.
将您的方法签名更改为:
open class func drawText(context: CGContext, text: String, point: CGPoint,
align: NSTextAlignment, attributes: [NSAttributedString.Key : Any]?)
Run Code Online (Sandbox Code Playgroud)
编辑:更新为Swift 4.2!NSAttributedStringKey已重命名为NSAttributedString.Key.
你的atrribute论点在drawText函数中是不正确的.
更改
open class func drawText(context: CGContext, text: String, point: CGPoint, align: NSTextAlignment, attributes: [String : AnyObject]?)
Run Code Online (Sandbox Code Playgroud)
至
open class func drawText(context: CGContext, text: String, point: CGPoint, align: NSTextAlignment, attributes: [NSAttributedStringKey : Any]?)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14870 次 |
| 最近记录: |