Skip to content

Instantly share code, notes, and snippets.

@orklann
Created February 16, 2021 06:36
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 orklann/9b0fd62e4fd23d04f7ac22eed0971705 to your computer and use it in GitHub Desktop.
Save orklann/9b0fd62e4fd23d04f7ac22eed0971705 to your computer and use it in GitHub Desktop.
Old draw string method
/* == Old draw string method
*/
- (CGFloat)drawString:(NSString*)ch font:(NSFont*)font context:(CGContextRef)context {
NSMutableAttributedString *s = [[NSMutableAttributedString alloc] initWithString:ch];
[s addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 1)];
[s addAttribute:NSForegroundColorAttributeName value:[NSColor blackColor] range:NSMakeRange(0, 1)];
CFAttributedStringRef attrStr = (__bridge CFAttributedStringRef)(s);
CTLineRef line = CTLineCreateWithAttributedString(attrStr);
CTLineDraw(line, context);
CFArrayRef runs = CTLineGetGlyphRuns(line);
CTRunRef firstRun = CFArrayGetValueAtIndex(runs, 0);
CGSize size;
CTRunGetAdvances(firstRun, CFRangeMake(0, 1), &size);
CFRelease(line);
return size.width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment