8

CoreTextを使用してNSAttributedStringを表示できると聞きましたが、どのように(最も簡単な方法で)教えてもらえますか?

CATextLayerまたはOHAttributedLabelで応答しないでください。

このフォーラムにはこれについて多くの質問があることを知っていますが、答えが見つかりません

ありがとう!!

4

3 に答える 3

13

最も簡単な方法は?このようなもの:

CGContextRef context = UIGraphicsGetCurrentContext();

// Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

// Create a path to render text in
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds );

// An attributed string containing the text to render
NSAttributedString* attString = [[NSAttributedString alloc]
                                  initWithString:...];

// create the framesetter and render text
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString); 
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
                         CFRangeMake(0, [attString length]), path, NULL);

CTFrameDraw(frame, context);

// Clean up
CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);
于 2011-11-30T11:05:03.733 に答える
10

最も簡単な方法(Core Textを使用)は次のとおりだと思います。

 // Create the CTLine with the attributed string
 CTLineRef line = CTLineCreateWithAttributedString(attrString); 

 // Set text position and draw the line into the graphics context called context
 CGContextSetTextPosition(context, x,  y);
 CTLineDraw(line, context);

 // Clean up
 CFRelease(line);

大量のテキストを描画する場合は Framesetter を使用する方が効率的ですが、少量のテキスト (ラベルなど) を表示するだけで、パスやフレーム (によって自動的に行われるためCTLineDraw)。

于 2012-03-11T17:28:47.960 に答える