2012-01-23 3 views
1

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

いくつかの基本的な質問: 2.テキストは何とか私はCGAffineTransformを使用して固定しました倒立出ているCATextLayer枠を設定するための位置とテキストのサイズを得るが、そのハック修正し、私が望む方法 1テキストが逆さまになっている実際の理由を知る。 3. NSAttributedStringを使用しているテキストの境界線の幅と色を設定する必要がありますが、テキストサイズの設定(増減)は画面に反映されません。

EDIT 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]; 

EDIT 2:ウィキの例はCTLineを使用し、私はCATextLayerを使用しています。 Reason :: CATextLayerは、中国語、日本語、ヒンディー語、ロシア語、および異なる言語のすべての言語を表示するために、実行時にテキストを変更できます。また、CATextLayerはCoreTextよりも明瞭にレンダリングするようです。

私が今直面している問題は、[text drawInContext:ctx]行で表示されるテキストが上から切り捨てられていることです。フレームとテキストのフォントサイズを確認しました。フォントサイズは16ピクセル、フレームの高さは17.768です。その理由は分かりません。それが原因でCGContextsに使用ひっくり返し座標空間であり、事前に

// 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]; 

おかげ

ニック

答えて

1

。詳細な説明については、hereを参照してください。

EDIT: あなたのコンテキストがテキストを描画する前に変換する位置を制御する:

CGContextTranslateCTM(context, x, y); 

はEDIT 2:

あなたがCTFontRef、ないUIFontを使用する必要があるように見えます。例については、hereを参照してください(Gordon Apple Wed Jun 23 10:40:23 2010の記事を参照してください)。

EDIT 3:withFont:

あなたは、具体的NSAttributedStringを使用する必要がない限り、私ははるかに簡単NSStringのdrawInRectを使用することをお勧めしますlineBreakMode:アライメント:方法。それはあなたのためCATextLayerを作成しますが、使用することがはるかに簡単です:

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

EDIT 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]; 
+0

クール:

このようなあなたの辞書を設定してみてください。座標空間は完璧です。しかし、NSAttributedStringで設定された属性に基づいてCATextLayerのサイズを変更することや、CATextLayerの.fontSizeプロパティを設定することはできません。 – nikj

+0

位置を制御する方法を示すために編集しました。サイズに関しては、何が間違っているのか分かりません。フォントは正しいですか?どのサイズですか、どのサイズを作ろうとしていますか? –

+0

こんにちはジェシュア、私はポストを更新しました。どうぞご覧ください。 – nikj

関連する問題