テキストを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];
前もって感謝します
ニック