繁体   English   中英

无法更改NSMutableAttributedString的颜色

[英]Can't change color of NSMutableAttributedString

我使用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)];

不幸的是,颜色没有改变。 在此先感谢您的帮助。

我添加我的评论作为答案,因为遵循它可以解决问题:

将属性添加到字符串后,必须重新绘制。 基本上,您在屏幕上看到的不是可更改的NSString对象,而是它读取的对象的图形绘图。 如果更改对象,则必须告诉它再次绘制对象。

因此,问题的答案是,您每次尝试更改有关所显示字符串的任何内容时都需要调用图形代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM