Created
February 16, 2021 06:36
-
-
Save orklann/9b0fd62e4fd23d04f7ac22eed0971705 to your computer and use it in GitHub Desktop.
Old draw string method
This file contains 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
/* == 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