CoreTextを使用してNSAttributedStringを表示できると聞きましたが、どのように(最も簡単な方法で)教えてもらえますか?
CATextLayerまたはOHAttributedLabelで応答しないでください。
このフォーラムにはこれについて多くの質問があることを知っていますが、答えが見つかりません
ありがとう!!
CoreTextを使用してNSAttributedStringを表示できると聞きましたが、どのように(最も簡単な方法で)教えてもらえますか?
CATextLayerまたはOHAttributedLabelで応答しないでください。
このフォーラムにはこれについて多くの質問があることを知っていますが、答えが見つかりません
ありがとう!!
最も簡単な方法は?このようなもの:
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);
最も簡単な方法(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
)。