Swift:在 CFArray 中提取/向下转换基于 CFType 的 CoreText 类型

2024-01-13

我正在尝试移植的元素核心动画文本 https://developer.apple.com/library/mac/samplecode/CoreAnimationText/Introduction/Intro.html示例到 Swift。但我不知道如何从数组中提取或向下转换 CTRun 的元素,以便将它们传递给期望并作用于 Swift 化的 CTRun 类型的函数。我要么收到运行时错误,要么收到下面操场片段中的链接错误

import CoreText
import QuartzCore

let text = NSAttributedString(string: "hello")
var line: CTLine = CTLineCreateWithAttributedString(text)

var ctRuns:CFArray = CTLineGetGlyphRuns(line)

let nsRuns:Array<AnyObject> = ctRuns as NSArray
nsRuns.count // == 1
// Playground execution failed: error: error: Couldn't lookup symbols:_OBJC_CLASS_$_CTRun
let nsRun = nsRuns[0] as CTRun
nsRun.self
println(nsRun.self)

let anyRuns = nsRuns as AnyObject[]
// can't unwrap Optional null
let anyRun = anyRuns[0] as CTRun
anyRun.self
println(anyRun.self)


let cft:AnyObject = anyRuns[0]
// CTRun is not contstructable with AnyObject
let runGlyphCount = CTRunGetGlyphCount(CTRun(cft));


// Can't unwrap Optional.None
let concreteRuns = anyRuns as CTRun[]
let concreteRun = concreteRuns[0] as CTRun
concreteRun.self
println(concreteRun.self)

有什么想法吗——我错过了一些明显的东西吗?来自 WWDC 会议和互操作指南 https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html,我被引导相信“Swift 就可以解决这个问题”。


根据开发论坛 https://devforums.apple.com/message/1061792#1061792,此功能在 6.1 中实现。

所有示例行均编译无错误并按预期工作 最新的 Xcode 6.1 (6A1052c) 。

与 CF 对象的互操作性正在改善。你可能会找到另一个 问题,在这种情况下,它将被报告为错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift:在 CFArray 中提取/向下转换基于 CFType 的 CoreText 类型 的相关文章

随机推荐