我使用 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)];
不幸的是,颜色没有改变。在此先感谢您的帮助。