A UITextView has some text with black color and some with red color. I want to change only the black color to white color. I have no idea how to select only the black color and change it to white color.
Asked
Active
Viewed 36 times
0
-
NSAttributedString + enumerate the foreground attribute. – Larme May 14 '19 at 15:27
-
You can check how to enumerate https://stackoverflow.com/questions/56021846/toggle-selectedrange-attributes-in-uitextview/56056187#56056187 https://stackoverflow.com/questions/56102521/change-only-fontsize-of-nsattributedstring it's on the font attribute, but you should get the idea. – Larme May 14 '19 at 15:28
1 Answers
0
Use method enumerateAttribute:inRange:options:usingBlock:
Example 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
}];

arts
- 118
- 4