1

テキストをCGContextに描画する必要があり、CATextLayerを使用してテキストを設定したいと思います。誰かがそれがどのように行われるかについて私にいくつかのステップを教えてもらえますか?私はそれのためにいくつかのコードを書きましたが、それの正確さはわかりません(私はiPhone開発に慣れていないので)。

いくつかの基本的な質問:1。CATextLayerフレームを設定するためのテキストの位置とサイズを取得する方法2. CGAffineTransformを使用して修正したテキストがどういうわけか反転して出てきますが、それはハッキーな修正であり、実際の理由を知りたいですテキストが反転して出てきます。3. NSAttributedStringを使用しているテキストの境界線の幅と色も設定する必要がありますが、テキストサイズの設定(増加または減少)は画面に反映されません。

編集1:

推奨事項に従ってコードを更新しましたが、CTFontRefを使用した後でもテキストのサイズが変更されません。また、iPhoneでは、テキストが上から少し切り捨てられています。何が間違って設定されているのか分かりますか?getTypographicBoundsから取得した値を確認したところ、すべて0(origin.x、origin.y、width、height)です。

getTypographicBoundsメソッド:

width = CTLineGetTypographicBounds(m_line, &ascent, &descent, &leading);

// Return text bounds
return CGRectMake(0, 0, width, ascent + descent);

変更されたコード:

CATextLayer*    text      = [[CATextLayer alloc] init];
CGColorSpaceRef rgbColor  = CGColorSpaceCreateDeviceRGB();
CGColorRef rgb            = CGColorCreate(rgbColor, m_fill_color);
text.foregroundColor      = rgb;
text.frame                = CGRectMake([self getTypographicBounds].origin.x, [self getTypographicBounds].origin.y, [self getTypographicBounds].size.width + 2*m_padding, [self getTypographicBounds].size.height + 2*m_padding);
text.wrapped              = YES;

    NSMutableDictionary *stringAttributes = [NSMutableDictionary dictionary];

    CTFontRef aCFFont = CTFontCreateWithName ((CFStringRef)m_font_name, 30.0, NULL);

    // Set a negative width so we get both stroke and fill to show
    [stringAttributes setObject: (NSObject*)aCFFont forKey: (id)kCTFontNameAttribute];
    [stringAttributes setObject: [NSNumber numberWithFloat: -3 ] forKey: (id)kCTStrokeWidthAttributeName];
    [stringAttributes setObject: [UIColor whiteColor] forKey: (id)kCTStrokeColorAttributeName];

    NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:m_text
                                                                       attributes:stringAttributes];

    text.string = (NSString*)stringToDraw;
    [text drawInContext:ctx];

編集2:wikiの例ではCTLineを使用していますが、私はCATextLayerを使用しています。理由::CATextLayerを使用すると、実行時にテキストを変更して、中国語、日本語、ヒンディー語、ロシア語、およびさまざまなスクリプトを使用するすべての言語を表示できます。また、CATextLayerはCoreTextよりも明確にレンダリングされるようです。

私が今直面している問題は、[text drawInContext:ctx]行で表示されているテキストが上から切り捨てられていることです。フレームとテキストのフォントサイズを確認しました。フォントサイズは16ピクセル、フレームの高さは17.768なので、なぜ切り捨てられるのかわかりません。

// Ensure CTLine is up-to-date
if (m_is_dirty)
    [self recomputeLine];

// Get text metrics
CGFloat width;
CGFloat ascent;
CGFloat descent;
CGFloat leading;

width = CTLineGetTypographicBounds(m_line, &ascent, &descent, &leading);

// Position text so that top of text aligns with top of layer
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity );

// Setup text drawing style
CGContextSetRGBFillColor   (ctx, m_fill_color  [0], m_fill_color  [1], m_fill_color  [2], 1.0);
CGContextSetRGBStrokeColor (ctx, m_stroke_color[0], m_stroke_color[1], m_stroke_color[2], 1.0);
CGContextSetFont           (ctx, m_font        );
CGContextSetFontSize       (ctx, m_font_size   );
CGContextSetLineWidth      (ctx, m_stroke_width);

CATextLayer*    text      = [[CATextLayer alloc] init];
CGColorSpaceRef rgbColor  = CGColorSpaceCreateDeviceRGB();
CGColorRef rgb            = CGColorCreate(rgbColor, m_fill_color);
text.foregroundColor      = rgb;
text.fontSize = m_font_size;
text.font = m_font;
text.frame                = CGRectMake([self getTypographicBounds].origin.x, [self getTypographicBounds].origin.y, [self getTypographicBounds].size.width, [self getTypographicBounds].size.height);
text.wrapped              = YES;

NSMutableDictionary *stringAttributes = [NSMutableDictionary dictionary];


// Set a negative width so we get both stroke and fill to show
[stringAttributes setObject: [UIFont fontWithName:m_font_name size:m_font_size] forKey: (id)kCTFontNameAttribute];
[stringAttributes setObject: [NSNumber numberWithFloat: -3 ] forKey: (id)kCTStrokeWidthAttributeName];
[stringAttributes setObject: [UIColor whiteColor] forKey: (id)kCTStrokeColorAttributeName];

NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:m_text
                                                                       attributes:stringAttributes];

text.string = (NSString*)stringToDraw;        
[text drawInContext:ctx];

前もって感謝します

ニック

4

1 に答える 1

1

これは、CGContext に使用される反転された座標空間が原因です。詳細な説明 (図付き) については、こちらを参照してください。

編集: 位置を制御するには、テキストを描画する前にコンテキストを翻訳します:

CGContextTranslateCTM(context, x, y);

編集2:

UIFont ではなく、CTFontRef を使用する必要があるようです。例については、こちらを参照してください(Gordon Apple Wed Jun 23 Wed 10:40:23 2010 の投稿を参照)。

編集3:

特に NSAttributedString を使用する必要がない限り、より単純な NSString drawInRect:withFont:lineBreakMode:alignment: メソッドを使用することをお勧めします。CATextLayer を作成しますが、使い方ははるかに簡単です。

    UIFont *font = [UIFont fontWithName:m_font_name size:30.f];
    [m_text drawInRect:text.frame  withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];

編集4:

次のように辞書を設定してみてください。

    CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)fontName, pointSize,NULL);
    CGColorRef color = textColor.CGColor;

    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    (__bridge id)ctFont, (id)kCTFontAttributeName,
                                    color, (id)kCTForegroundColorAttributeName,nil];
于 2012-01-23T09:11:18.957 に答える