0

CoreText を使用して、四角形を塗りつぶし、画像を挿入する領域を囲む次のテキストを作成しました。

attrString = [[NSMutableAttributedString alloc] initWithString:string];
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTFontAttributeName, font);
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTForegroundColorAttributeName, ForegroundTextColor);
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTParagraphStyleAttributeName, paragraphStyle);

/* Get a framesetter to draw the actual text */
CTFramesetterRef fs = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) attrString);
CTFrameRef frame = CTFramesetterCreateFrame(fs, CFRangeMake(0, attrString.length), pathToRenderIn, NULL);

/* Draw the text */
CTFrameDraw(frame, ctx);

/* Draw the text */
CTFrameDraw(frame, ctx);

後の方法では、添付のステートメントを使用して、テキストのサブセットの色を変更しています。

[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(startRangeValue, endRangeValue)];

残念ながら色は変わりません。よろしくお願いします。

4

1 に答える 1

1

問題を解決したため、コメントを回答として追加しています。

文字列に属性を追加したら、再描画する必要があります。基本的に、画面に表示されるのは変更可能な NSString オブジェクトではなく、読み取ったオブジェクトのグラフィカルな描画です。オブジェクトを変更する場合は、オブジェクトを再度描画するように指示する必要があります。

したがって、問題に対する答えは、表示している文字列について何かを変更しようとするたびに、描画コードを呼び出す必要があるということです。

于 2013-06-14T22:31:55.947 に答える