1

iOS で CoreText を使用して OpenGL テクスチャをレンダリングしようとしています。CoreText は CoreGraphics Bitmap コンテキストでレンダリングされ、次に を使用して OpenGL にロードされますglTexImage2D

RGB カラー スペースを使用してビットマップ コンテキストを作成すると、すべて正常に動作します。

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
uint8_t *data = (uint8_t *)calloc(height, 4 * width);
CGContextRef context = CGBitmapContextCreate(data, width, height, 8, 4 * width, colorSpace, kCGImageAlphaNoneSkipLast);
CGColorSpaceRelease(colorSpace);

ただし、グレースケールのみの色空間を使用したいと思います。私がするとき、テキストは表示されません。

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
uint8_t *data = (uint8_t *)calloc(height, width);
CGContextRef context = CGBitmapContextCreate(data, width, height, 8, width, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);

私がレンダリングしているテキストは黒です。どちらの場合も、CoreGraphics メソッドを使用してコンテキスト内で描画できます。

次のコードを使用してテキストを描画します。

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)text);

CGSize dimensions = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [text length]), NULL, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX), NULL);

CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, dimensions.height);
CGContextScaleCTM(context, 1.0, -1.0);

CGRect box = CGRectMake(0, 0, dimensions.width, dimensions.height);

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, box );

CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [text length]), path, NULL);

CTFrameDraw(frame, context);

CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);

これを機能させるための CoreText の特別な設定はありますか?

ありがとう

4

1 に答える 1

0

OK、問題を再現できました。NSAttributeString プロパティ ディクショナリでは、UIColor (NSColor?) ではなく、CGColorRef を使用する必要があります。このようにして、CGContextAPI は色空間に応じて色を処理する方法を認識します。以下を実行するだけで、問題なく実行できます。

CGColorRef col = [UIColor blackColor].CGColor;
NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                          //whatever attributes you need
                                          col, kCTForegroundColorAttributeName,
                                          nil];

NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:yourText
                                                                   attributes:attributesDict];

これが役立つことを願っています。うまくいくかどうか教えてください!

于 2013-05-24T15:55:47.050 に答える