使用 Swift 升级到 Xcode 6.3 后无法创建 CFAttributedString

Trouble creating CFAttributedString after upgrade to Xcode 6.3 with Swift

我的代码将 CFAttributedString 绘制到图形上下文中,在更新到 Xcode 6.3 之前工作正常。

现在,升级后,出现以下错误:

'_' is not convertible to 'CFString!'

'[String : AnyObject]' is not convertible to '[String : AnyObject]'

对于定义字符串属性的代码行:

let attributes : [String:AnyObject] = [
            kCTForegroundColorAttributeName:UIColor.darkGrayColor().CGColor,
            kCTFontAttributeName:font
        ]

我是这样画文字的:

var attrString = CFAttributedStringCreate(nil,myString,attributes)
        var line = CTLineCreateWithAttributedString(attrString)
        var lineWidth = CTLineGetBoundsWithOptions(line, CTLineBoundsOptions.UseGlyphPathBounds).width
        var lineHeight = CTLineGetBoundsWithOptions(line, CTLineBoundsOptions.UseGlyphPathBounds).height
        CGContextSetTextMatrix(context, transformScale)
        CGContextSetTextPosition(context, (rect.width - lineWidth)/2, rect.height - lineHeight*1.5)
        CTLineDraw(line, context)

我尝试在 kCTForegroundColorAttributeName 和 kCTFontAttributeName 之后添加 "as String" 进行转换。这消除了错误,但字符串似乎没有获得属性。

我通过更改格式并将 kCTForegroundColorAttributeName 替换为 NSForegroundColorAttributeName

解决了这个问题

这里是固定线路:

let attributes: [String: AnyObject] = [
            NSForegroundColorAttributeName : UIColor.darkGrayColor().CGColor,
            NSFontAttributeName : font
        ]