gpt4 book ai didi

iphone - 无法更改 NSMutableAttributedString 的颜色

转载 作者:行者123 更新时间:2023-11-29 03:50:42 25 4
gpt4 key购买 nike

我使用 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 对象,它是它读取的对象的图形绘制。如果更改对象,则必须告诉它再次绘制对象。

所以问题的答案是,每次尝试更改正在显示的字符串的任何内容时,都需要调用绘图代码。

关于iphone - 无法更改 NSMutableAttributedString 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17114685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com