Different colors in a UITextView

I am trying to add a string with different colors to a UITextView. I wrote this code

NSMutableAttributedString* attString =
   [[NSMutableAttributedString alloc]initWithString:view.text]; //view is my UITextView
[attString addAttribute:(NSString*)kCTForegroundColorAttributeName 
                  value:[UIColor greenColor] 
                  range:(NSRange){attString.length-8, 8}];
view.attributedText = attString;

Maybe this is the wrong attribute, can you tell me which attribute changes color for the text?

+1
source share
1 answer

Try using NSForegroundColorAttributeNameinstead kCTForegroundColorAttributeName.

NSAttributedString UIKit Help Addendum: Character Attributes

+3
source

Source: https://habr.com/ru/post/1533902/


All Articles