【问题标题】:Trouble creating CFAttributedString after upgrade to Xcode 6.3 with Swift使用 Swift 升级到 Xcode 6.3 后无法创建 CFAttributedString
【发布时间】:2015-04-12 19:16:13
【问题描述】:

我的代码将 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”来投射它们。这消除了错误,但字符串似乎没有获得属性。

【问题讨论】:

  • 出于好奇,你想做什么?为什么将这些存储为字符串?这没有任何意义,您将颜色的名称作为键,然后将字体作为值?
  • 我正在创建一个带有文本的图形。查看我对如何绘制文本的编辑。
  • 我喜欢总是展示你想要做什么。也许用代码更新帖子。可能有更好的方法。
  • 查看我的编辑,在“这是我绘制文本的方式”行下方
  • 你为什么使用 CF 类而不是 NSAttributedString?

标签: xcode swift core-foundation


【解决方案1】:

我通过更改格式并将 kCTForegroundColorAttributeName 替换为 NSForegroundColorAttributeName 解决了这个问题

这里是固定线路:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2011-07-21
    相关资源
    最近更新 更多