23

I have a UITextView where the user can input text. Say the user has already inputted a value. How can I change the font color of that already typed text and future typed text to a different one with the click of a button?

I already have it set up where I can change the color of the text but it only works if I choose a color BEFORE I start typing. After I start typing and I attempt to change the color, it doesn't do anything.

I have this:

-(IBAction)changeInkColor:(id)sender
{
    [inkTextField setTextColor:[UIColor greenColor]]; 
    inkTextField.text=@"text";
}

and that actually works and displays "text" in green only if there is no text already in the view. However if I type something in and then hit this button, nothing happens.

Snowman
  • 31,411
  • 46
  • 180
  • 303

6 Answers6

24

I just tried that and I had no problems. I set up a button that called [myTextView setTextColor:[UIColor redColor]];

After typing a bit with black text color, I pressed the button, and everything turned red. Then I continued typing, all in red.

Are you using setTextColor: to do this also?

Sam B
  • 27,273
  • 15
  • 84
  • 121
pinerd314159
  • 534
  • 4
  • 10
19

In the end, setTextColor: is the answer, there's an important detail missing from the earlier answers: To get this to work in iOS 8, I had to set the color =after= I set the text.

Hence,

- (void)viewDidLoad
{
    [super viewDidLoad];

    _myTextView.textColor = [UIColor whiteColor];
    _myTextView.text   = @"yadda yadda yadda...";

    // etc., snip

Did NOT work, while

- (void)viewDidLoad
{
    [super viewDidLoad];

    _myTextView.text   = @"yadda yadda yadda...";
    _myTextView.textColor = [UIColor whiteColor];

    // etc., snip

DID work. This strikes me as a bug in iOS 8, which I will write up.

Olie
  • 24,597
  • 18
  • 99
  • 131
  • 2
    FYI, this is still the solution. It fixed my problem. If I set the text before the textColor, the text stays black. If I set the color after the text, it works fine. – matcartmill Sep 19 '15 at 13:03
  • 1
    @matcartmill, You have a typo in your comment. Your third and fourth sentences describe the same scenario but with different outcomes. Set the text (1) before the textColor (2) --> text is black. Set the text (1) and then the color (2) --> it works fine. Your last sentence is the correct one. – Gary Z Feb 28 '16 at 08:28
  • 1
    Should be the accepted answer. **Setting text color after text fixed my problem**. – Borzh May 30 '17 at 02:15
1

You can fix this (at least in Xcode 8.2) in IB by toggling the textColor button (6th button in the row). With this button selected, if you set the textColor programmatically before you enter text in the view, the color will "stick." I have not found an elegant way to set this on the text view programatically.

UITextView IB settings

However you can also work around this in code:

textView.text = @" ";
textView.textColor = UIColor.redColor;
textView.text = @"";
Rand
  • 165
  • 1
  • 1
  • 12
0

I had the same problem as you, then I realised that the text was not changing colour, and was red by default because I was doing:

placeholder = @"Welcome";

instead of

 WelcomeField.text = @"Welcome";

Hope it helps

user2014474
  • 1,117
  • 5
  • 19
  • 36
0

use attributed text of UITextField

NSAttributedString *string = [[NSAttributedString alloc]initWithString:self.textField.text attributes:@{NSForegroundColorAttributeName:color}];
self.textField.attributedText = string;
Yllow
  • 326
  • 4
  • 7
0

Very interestingly, while

[_nameLabel setTextColor: [UIColor redColor]];

didn't work,

_nameLabel.textColor = [UIColor redColor]];

worked. Hope it helps for others. Cheers.

Eray Alparslan
  • 806
  • 7
  • 12