Skip to content

Instantly share code, notes, and snippets.

@7gano
Last active September 30, 2015 11:57
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7gano/9248eb3565e01e58ab53 to your computer and use it in GitHub Desktop.
Save 7gano/9248eb3565e01e58ab53 to your computer and use it in GitHub Desktop.
グリフのパス(ベクター)を取得して描画する
@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];
}
@7gano
Copy link
Author

7gano commented Oct 24, 2013

snowman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment