【问题标题】:How to change some text colors in multiple colored UITextView如何在多种颜色的 UITextView 中更改某些文本颜色
【发布时间】:2019-10-01 15:45:43
【问题描述】:

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

【问题讨论】:

标签: objective-c uitextview textcolor


【解决方案1】:

使用方法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
}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多