我正在开发一个使用CoreText的项目;我需要初始化CTRunDelegateCallbacks
:
var imageCallback = CTRunDelegateCallbacks(version: kCTRunDelegateCurrentVersion, dealloc: { (refCon) -> Void in
print("RunDelegate dealloc")
}, getAscent: { ( refCon) -> CGFloat in
return 0
}, getDescent: { (refCon) -> CGFloat in
return 0
}) { (refCon) -> CGFloat in
return 0
}
参数refCon
是UnsafeMutablePointer<Void>
类型,在C中也称为void *
类型。我想获取指针的原始值。怎么做?
答案 0 :(得分:1)
我不建议将指针转换为非指针类型。我会告诉你如何在这个答案的最后做到这一点,但首先我会告诉你应该如何处理refCon
。
创建一个结构来保存您需要传递给回调的任何信息。例如:
struct MyRunExtent {
let ascent: CGFloat
let descent: CGFloat
let width: CGFloat
}
然后使用其UnsafeMutablePointer
类方法创建alloc
,并初始化分配的存储:
let extentBuffer = UnsafeMutablePointer<MyRunExtent>.alloc(1)
extentBuffer.initialize(MyRunExtent(ascent: 12, descent: 4, width: 10))
在你的回调中,将指针参数转换为UnsafePointer<MyRunExtent>
并从memory
中提取所需内容:
var callbacks = CTRunDelegateCallbacks(version: kCTRunDelegateVersion1, dealloc: { pointer in
pointer.dealloc(1)
}, getAscent: { pointer in
return UnsafePointer<MyRunExtent>(pointer).memory.ascent
}, getDescent: { pointer in
return UnsafePointer<MyRunExtent>(pointer).memory.descent
}, getWidth: { pointer in
return UnsafePointer<MyRunExtent>(pointer).memory.width
})
现在,您可以使用callbacks
和extentBuffer
:
let delegate = CTRunDelegateCreate(&callbacks, extentBuffer)!
这是一个测试:
let richText = NSMutableAttributedString(string: "hello \u{FFFC} world")
richText.addAttribute(kCTRunDelegateAttributeName as String, value: delegate, range: NSMakeRange(6, 1))
let line = CTLineCreateWithAttributedString(richText)
let runs = (CTLineGetGlyphRuns(line) as [AnyObject]).map { $0 as! CTRun }
runs.forEach {
var ascent = CGFloat(0), descent = CGFloat(0), leading = CGFloat(0)
let width = CTRunGetTypographicBounds($0, CFRangeMake(0, 0), &ascent, &descent, &leading)
print("width:\(width) ascent:\(ascent) descent:\(descent) leading:\(leading)")
}
输出(在操场上):
2015-12-21 12:26:00.505 iOS Playground[17525:8055669] -[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x7f94bcb01dc0
width:28.6875 ascent:9.240234375 descent:2.759765625 leading:0.0
width:10.0 ascent:12.0 descent:4.0 leading:0.0
width:32.009765625 ascent:9.240234375 descent:2.759765625 leading:0.0
第一行输出是因为操场执行过程无法对委托进行编码以发送回Xcode进行显示,结果证明是无害的。无论如何,您可以看到中间运行的界限是使用我的回调和我的extentBuffer
的内容计算的。
您可以通过这种方式获取指针的“原始值”,如果指针和Int
在运行的系统上的大小相同:
let rawValue = unsafeBitCast(refCon, Int.self)
如果它们的大小不同,您将在运行时遇到致命错误。
您可以通过这种方式将其投放到CGFloat
,如果指针和CGFloat
的尺寸相同:
let float = unsafeBitCast(refCon, CGFloat.self)