如何在多种颜色的 UITextView 中更改某些文本颜色

How to change some text colors in multiple colored UITextView

一个 UITextView 有一些文本是黑色的,一些文本是红色的。我只想将黑色更改为白色。我不知道如何 select 只有黑色并将其更改为白色。

使用方法enumerateAttribute:inRange:options:usingBlock:

示例Obj-C:

NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"Test Attributed String"];
[string addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, string.length-3)];
[string enumerateAttribute:NSStrokeColorAttributeName inRange:NSMakeRange(0, string.length) options:NSAttributedStringEnumerationReverse usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
    // value is color
}];