10

私はiOS初心者です。「B」というアルファベットに続く UIBezierPath を作成するにはどうすればよいですか。目的は、このパスに沿ってタッチを追跡することです。

前もって感謝します。

4

2 に答える 2

9

CoreText.framework は、単語のパスを取得するメソッドを提供します

http://www.codeproject.com/Articles/109729/Low-level-text-renderingを参照してください

Ole Begemann によって作成されたコード例。AnimatedPath という名前のデモのダウンロード URL を忘れてしまいました。

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!」を置き換えます。「あなたが必要とする言葉」で。

于 2012-10-31T12:41:46.223 に答える
0

Quartz2D での描画には、PaintCode ( http://www.paintcodeapp.com/ ) という OSX アプリを使用します。基本的には、描画のクォーツコードを生成するベクター描画アプリです。それは実際にはかなり素晴らしいです。Opacity という同様のアプリがありますが、試したことはありません。

このようなアプリでは、ガイドとして背景に B を配置し、その上に BezierPath を描画できます。完了したら、生成されたコードをコピーしてプロジェクトに貼り付けます。

それが役に立てば幸い。

于 2012-05-25T06:59:43.623 に答える