Created
March 29, 2018 07:54
-
-
Save kbinani/ebba9eec655b65823343c1a69d6dacbb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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