ios NSAttributedString 在字符串末尾更改颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19123503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:55:59  来源:igfitidea点击:

NSAttributedString change color at end of string

ioscolorsnsattributedstring

提问by Raconteur

This must be an easy thing to do, but I cannot figure it out.

这一定是一件容易的事情,但我无法弄清楚。

I have a NSMutableAttributedString that has, for example, "This is a test" in it. I want to color the word "test" blue, which I do with this:

我有一个 NSMutableAttributedString ,例如,“这是一个测试”。我想把“测试”这个词涂成蓝色,我是这样做的:

NSMutableAttributedString *coloredText = [[NSMutableAttributedString alloc] initWithString:@"This is a test"];

[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(10,4)];

That works just fine. But now I want to set the text color back to black for anything typed after "test".

这工作得很好。但是现在我想将“测试”后键入的任何内容的文本颜色设置回黑色。

If I do:

如果我做:

[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(coloredText.string.length, 1)];

I get an objectAtIndex:effectiveRange: out of bounds error. Assumedly because the range extends beyond the length of the string.

我得到一个 objectAtIndex:effectiveRange: out of bounds 错误。推测是因为范围超出了字符串的长度。

If I do:

如果我做:

[coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(coloredText.string.length, 0)];

The error goes away but typing after the word "test" remains blue.

错误消失了,但在“test”这个词之后输入仍然是蓝色的。

How do I set the current color at the insertion point when it is at the end of the string??

插入点在字符串末尾时如何设置当前颜色?

Cheers for any input.

为任何输入干杯。

采纳答案by kkodev

You need to recalculate the attributes if the text changes, because their effective range doesn't automatically change with the length of text.

如果文本发生变化,您需要重新计算属性,因为它们的有效范围不会随着文本长度而自动改变。

回答by Raconteur

In case someone else stumbles upon this, I wanted to post the code I used to solve the problem. I ended up using Kamil's suggestion and adding:

如果其他人偶然发现了这一点,我想发布我用来解决问题的代码。我最终使用了 Kamil 的建议并补充说:

NSAttributedString *selectedString = [textView.attributedText attributedSubstringFromRange:NSMakeRange(textView.attributedText.string.length - 1, 1)];
    __block BOOL isBlue = NO;

    [selectedString enumerateAttributesInRange:NSMakeRange(0, [selectedString length]) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) {
        isBlue = [[attributes objectForKey:NSForegroundColorAttributeName] isEqual:[UIColor blueColor]];
    }];

    if (isBlue) {
        NSMutableAttributedString *coloredText = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
        [coloredText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(textView.attributedText.string.length - 1, 1)];
        textView.attributedText = coloredText;
    }

to the text changed handler.

到文本更改处理程序。