我是iOS新手。如何使我遵循字母表的UIBezierPath说“B”。目标是跟踪沿着这条道路的接触。
提前致谢。
答案 0 :(得分:9)
CoreText.framework提供了获取单词
路径的方法请参阅http://www.codeproject.com/Articles/109729/Low-level-text-rendering
Ole Begemann创建的代码示例。抱歉,我忘了为名为AnimatedPath的演示下载网址。
CGMutablePathRef letters = CGPathCreateMutable();
CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 72.0f, NULL);
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
(id)font, kCTFontAttributeName,
nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Hello World!"
attributes:attrs];
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
CFArrayRef runArray = CTLineGetGlyphRuns(line);
// for each RUN
for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
{
// Get FONT for this run
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
// for each GLYPH in run
for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
{
// get Glyph & Glyph-data
CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
CGGlyph glyph;
CGPoint position;
CTRunGetGlyphs(run, thisGlyphRange, &glyph);
CTRunGetPositions(run, thisGlyphRange, &position);
// Get PATH of outline
{
CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
CGPathAddPath(letters, &t, letter);
CGPathRelease(letter);
}
}
}
CFRelease(line);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path appendPath:[UIBezierPath bezierPathWithCGPath:letters]];
CGPathRelease(letters);
CFRelease(font);
将“Hello World!”改为“你需要的词”。
答案 1 :(得分:0)
对于Quartz2D中的绘图,我使用名为PaintCode(http://www.paintcodeapp.com/)的OSX应用程序。它基本上是一个矢量绘图应用程序,它生成你所做的绘图的石英代码。实际上它非常棒。有一个类似的应用程序叫做Opacity,但我没试过。
使用此类应用程序,您可以在后台输入B作为指南,并在其上绘制BezierPath。完成后,只需复制生成的代码并将其粘贴到项目中即可。
希望它有所帮助。