Change font color of UITextView

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.

  • Objective-C: Assertion vs. Exception vs. Error
  • UITableView Section Index overlapping row delete button
  • iOS7: background task (“myapp” has active assertions beyond permitted time)
  • Application Tests VS Logic Tests
  • Why does this property need the 'retain'?
  • Instagram open UTI directly
  • I have this:

        [inkTextField setTextColor:[UIColor greenColor]]; 

    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.

    5 Solutions Collect From Internet About “Change font color of UITextView”

    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?

    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.


    - (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.

    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 = @"";

    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

    use attributed text of UITextField

    NSAttributedString *string = [[NSAttributedString alloc]initWithString:self.textField.text attributes:@{NSForegroundColorAttributeName:color}];
    self.textField.attributedText = string;