Skip to content

Instantly share code, notes, and snippets.

@kbinani
Created March 29, 2018 07:54
Show Gist options
  • Save kbinani/ebba9eec655b65823343c1a69d6dacbb to your computer and use it in GitHub Desktop.
Save kbinani/ebba9eec655b65823343c1a69d6dacbb to your computer and use it in GitHub Desktop.
// NG
func copyTextTab(_ style: CTParagraphStyle) -> [CTTextTab] {
var tabStops: CFArray!
guard CTParagraphStyleGetValueForSpecifier(style, .tabStops, MemoryLayout<CFArray>.size, &tabStops) else {
return []
}
return (tabStops as NSArray)
.filter({ (tab) -> Bool in CFGetTypeID(tab as CFTypeRef) == CTTextTabGetTypeID() })
.map({ (it) -> CTTextTab in
let tab = it as! CTTextTab
let alignment = CTTextTabGetAlignment(tab)
let location = CTTextTabGetLocation(tab)
let options = CTTextTabGetOptions(tab)
return CTTextTabCreate(alignment, location, options)
})
}
}
// OK
func copyTextTab(_ style: CTParagraphStyle) -> [CTTextTab] {
let storage = UnsafeMutablePointer<CFArray>.allocate(capacity: MemoryLayout<CFArray>.size)
guard CTParagraphStyleGetValueForSpecifier(style, .tabStops, MemoryLayout<CFArray>.size, storage) else {
return []
}
guard CFGetTypeID(storage.pointee) == CFArrayGetTypeID() else {
return []
}
let result = (storage.pointee as NSArray)
.filter({ (it) -> Bool in CFGetTypeID(it as CFTypeRef!) == CTTextTabGetTypeID() })
.map({ (it) -> CTTextTab in
let tab = it as! CTTextTab
return CTTextTabCreate(CTTextTabGetAlignment(tab), CTTextTabGetLocation(tab), CTTextTabGetOptions(tab))
})
storage.deallocate(capacity: MemoryLayout<CFArray>.size)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment