我如何在Swift 4中修复此片段

  • 本文关键字:片段 Swift swift4
  • 更新时间 :
  • 英文 :


我正在尝试将文本属性添加到Swift 4中的绘制方法:

let textFontAttributes = [
            NSAttributedStringKey.font.rawValue: font,
            NSAttributedStringKey.foregroundColor: minuteTickColor,
            NSAttributedStringKey.paragraphStyle: textStyle
        ] as! [String : Any]
.....
var numberString:NSString = String(index) as NSString
numberString.draw(in: rect, withAttributes: textFontAttributes)

这没有编译,我无法弄清楚我在做什么错。

编译错误是:

Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedStringKey : Any]?'

已解决(仍在测试(

let textFontAttributes = [
                NSAttributedStringKey.font.rawValue: font,
                NSAttributedStringKey.foregroundColor: minuteTickColor,
                NSAttributedStringKey.paragraphStyle: textStyle
                ] as [AnyHashable : Any]
....
numberString.draw(at: point, withAttributes: textFontAttributes as! [NSAttributedStringKey : Any])

尝试以下:

let textFontAttributes = [
     NSFontAttributeName: font,
     NSForegroundColorAttributeName: minuteTickColor,
     NSParagraphStyleAttributeName: textStyle
]

最新更新