I needed UITextView where user can enter text in multiple color at the time of inserting. For example if he enter XYZ, then color will be red, if he enter ABC then it must be blue.
3 Answers
Not sure if this helps, as of IOS 6, you can now change the attributes which technically includes Fonts, and Colors. I was successful with Fonts change for part of the text, but seeing a challenge in changing the colour, anyhow, below is the way to go in case Apple is applying a change to this someday since AttributedText is a property of UITextView and should allow the same standards.
You can use (NSMutableAttributedString) as opposed to (NSString) in connection with .attributedText (as opposed to .text), see the example below, it's very doable, just create a new UITextView and then make do the delegate etc name it txtLog as per below:
NSMutableAttributedString *Temp = [[NSMutableAttributedString alloc] initWithString:@"Heider Sati... Hello World"];
[Temp addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:30.0f] range:NSMakeRange(0, 11)]; /* This will change the first 11 characters into the font above... */
[Temp addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(12, 15)]; /* This will change the color on the other characters starting from 12 to 15... */
txtLog.attributedText = Temp;
You can also use NSAttributedString instead of NSMutableAttributedString if you like, but I use the Mutable to append the string if needed, as it's mostly the case when you read and write into the UITextView field.
I hope this helps,
Kind Regards Heider Sati

- 693
- 10
- 25

- 2,476
- 26
- 28
-
getting crash while using your code. "out of bound". and `NSStrokeColorAttributeName` is also not working for me I use `NSForegroundColorAttributeName` instead of `NSStrokeColorAttributeName` – Mihir Oza Sep 11 '18 at 13:10
This answer was given prior the release of iOS 6 and the support of NSAttributedString in UITextViews.
Sorry this is not possible a UITextView is plain text only. You can only set text and font attributes globally applying to the whole text.
Have a look at UITextView with Syntax Highlighting, it's related to your situation. The first answer gives you a good overview and some suggestions how to solve your problem.

- 1
- 1

- 47,228
- 12
- 98
- 108
-
1Thanks Nick for you help. I solved it. Please have a look on https://github.com/enormego/EGOTextView – Mangesh Mar 19 '12 at 10:35
Usual UITextView does not allow to have text in multiple color. You should implement your own UITextView

- 21,660
- 14
- 87
- 109
-
Thanks George, Please have a look on http://github.com/enormego/EGOTextView. It is doing the same. – Mangesh Mar 19 '12 at 10:37