Skip to content

Instantly share code, notes, and snippets.

@andlabs
Created January 4, 2017 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andlabs/14f0eecd866cb26b07fc73af71639ef9 to your computer and use it in GitHub Desktop.
Save andlabs/14f0eecd866cb26b07fc73af71639ef9 to your computer and use it in GitHub Desktop.
import CoreFoundation
import CoreGraphics
import CoreText
var font = CTFontCreateWithName(("Lucida Grande" as! CFString), 12, nil)
var attrs = CFDictionaryCreateMutable(nil, 0,
[kCFCopyStringDictionaryKeyCallBacks],
[kCFTypeDictionaryValueCallBacks])
CFDictionaryAddValue(attrs,
Unmanaged.passUnretained(kCTFontAttributeName).toOpaque(),
Unmanaged.passUnretained(font).toOpaque())
var s = "This is a test string."
for cc in 0..<10 {
s += " This is a test string."
}
var cfas = CFAttributedStringCreate(nil, s as! CFString, nil)//attrs)
print("\(cfas)")
var fs = CTFramesetterCreateWithAttributedString(cfas!)
var range = CFRangeMake(0, CFAttributedStringGetLength(cfas))
var unused = CFRange()
var size = CTFramesetterSuggestFrameSizeWithConstraints(fs, range, nil,
CGSize.init(width: 200, height: CGFloat.greatestFiniteMagnitude),
&unused)
print("suggested size \(size)")
var rect = CGRect(origin: CGPoint.init(x: 5, y: 2.5), size: size)
print("path rect \(rect)")
var path = CGPath(rect: rect, transform: nil)
var frame = CTFramesetterCreateFrame(fs, range, path, nil)
var pathbb = path.boundingBoxOfPath
print("path bounding box \(pathbb)")
var lines = CTFrameGetLines(frame)
var n = CFArrayGetCount(lines)
range.length = 1
print("\(n) lines")
var th: CGFloat = 0
for i in 0..<n {
range.location = i
var p = CGPoint.zero
CTFrameGetLineOrigins(frame, range, &p)
var ll = CFArrayGetValueAtIndex(lines, i)
var l = Unmanaged<CTLine>.fromOpaque(ll!).takeUnretainedValue()
var asc: CGFloat = 0
var des: CGFloat = 0
var lea: CGFloat = 0
var wid = CTLineGetTypographicBounds(l, &asc, &des, &lea)
print("line \(i): \(lea)")
print("\ty+ascent \(p.y + asc)")
print("\ty-descent \(p.y - des)")
th += asc + des + lea
}
print("total height \(th)")
Optional(This is a test string. This is a test string. This is a test string. This is a test string. This is a test string. This is a test string. This is a test string. This is a test string. This is a test string. This is a test string. This is a test string.{
})
suggested size (199.634765625, 105.0)
path rect (5.0, 2.5, 199.634765625, 105.0)
path bounding box (5.0, 2.5, 199.634765625, 105.0)
7 lines
line 0: 0.0
y+ascent 102.240234375
y-descent 90.240234375
line 1: 0.0
y+ascent 87.240234375
y-descent 75.240234375
line 2: 0.0
y+ascent 72.240234375
y-descent 60.240234375
line 3: 0.0
y+ascent 57.240234375
y-descent 45.240234375
line 4: 0.0
y+ascent 42.240234375
y-descent 30.240234375
line 5: 0.0
y+ascent 27.240234375
y-descent 15.240234375
line 6: 0.0
y+ascent 12.240234375
y-descent 0.240234375
total height 84.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment