Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font color of UITextView

Tags:

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.

like image 673
Snowman Avatar asked Aug 20 '11 19:08

Snowman


People also ask

How do I change my Font color?

Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.

How do you change the Font color in a script?

Users can change the style of the element using the . style property. Also, we can change the specific style, such as element color, background color, size, etc. In our case, we will change the color attribute values to change the font color.

How do I change the textfield Font in Swift?

yes you can do it. [tFild_1 setFont:[UIFont fontWithName:@"Helvetica Neue" size:17]]; instead of "Helvetica Neue" use your font name.


2 Answers

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?

like image 89
pinerd314159 Avatar answered Sep 17 '22 19:09

pinerd314159


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.

like image 23
Olie Avatar answered Sep 17 '22 19:09

Olie