-
-
Save 7gano/9248eb3565e01e58ab53 to your computer and use it in GitHub Desktop.
グリフのパス(ベクター)を取得して描画する
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
@import CoreText; | |
@import QuartzCore; | |
@implementation RCViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
CTFontRef font = CTFontCreateWithName((CFStringRef)@"HiraKakuProN-W3", 60, NULL); | |
const UniChar characters[] = { [@"☃" characterAtIndex:0]}; | |
CGGlyph glyphs[1]; | |
CTFontGetGlyphsForCharacters(font, characters, glyphs, 1); | |
//この関数呼び出しが肝なのだ! | |
CGPathRef path = CTFontCreatePathForGlyph(font, glyphs[0], NULL); | |
CGRect boundingBox = CGPathGetBoundingBox(path); | |
NSLog(@"boundingBox = %@", NSStringFromCGRect(boundingBox)); | |
//boundingBox = {{8.3400002, -2.3999999}, {42.66, 53.100002}} | |
//なので、このままだとはみ出る。ので、CGAffineTransformTranslateでPathを移動する | |
CAShapeLayer* shapeLayer; | |
shapeLayer = [CAShapeLayer layer]; | |
shapeLayer.frame = boundingBox; | |
shapeLayer.position = self.view.center; | |
shapeLayer.geometryFlipped = YES; | |
shapeLayer.fillColor = [UIColor whiteColor].CGColor; | |
shapeLayer.backgroundColor = [UIColor blackColor].CGColor; | |
CGMutablePathRef newPath = CGPathCreateMutable(); | |
CGAffineTransform transform = CGAffineTransformIdentity; | |
transform = CGAffineTransformTranslate(transform, -boundingBox.origin.x, -boundingBox.origin.y); | |
CGPathAddPath(newPath, &transform, path); | |
shapeLayer.path = newPath; | |
CFRelease(newPath); | |
CFRelease(font); | |
CFRelease(path); | |
[self.view.layer addSublayer:shapeLayer]; | |
} |
Author
7gano
commented
Oct 24, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment