遵循字体形状的 iOS UIBezierPath

标签 ios character uibezierpath

我是 iOS 新手。如何让我成为一个遵循字母“B”的 UIBezierPath。目标是沿着这条路径跟踪触摸。

提前致谢。

最佳答案

CoreText.framework提供获取word路径的方法

参见 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!”与“你需要的词”。

关于遵循字体形状的 iOS UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10599355/

相关文章:

ios - ObjectMapper 数组错误

ios - UINavigationController 剪辑 subview

objective-c - 制作 UIBezierPath 的独立副本?

ios - FastPDFKit 为模拟器构建,但显示 arm7 的重复符号错误

ios - iOS使用数组过滤数组

java - 线程 "main"java.lang.StringIndexOutOfBoundsException : String index out of range: 4 中的异常

c - 如何将字符串中的前两个字符分配给 C 中的变量(Arduino)

c - 从 C 中的 fgetc() 获取字符

ios - UIGestureRecognizer 只为 subView 添加,但它也适用于 parentView?

ios - UIBezierPath 的颜色和线宽错误