Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView Cursor Color not changing iOS 7

I'm trying to set the cursor color of a UITextView based on a user's preferences. They select what color they want with a button.

By default, the textview's cursor color is white. When the user presses the button, it might change to green:

[_textView setTintColor:[UIColor greenColor]];
[_textView setTextColor:[UIColor greenColor]];

I am sure that this method call is working because the textview's text changes color, just not the cursor...

like image 448
Apollo Avatar asked May 18 '14 18:05

Apollo


3 Answers

I'm using small hack in UITextViewDelegate method:

func textViewDidBeginEditing(_ textView: UITextView) {
    let color = textView.tintColor
    textView.tintColor = .clear
    textView.tintColor = color
}

UPD

more technological way:

1) implement method in extension:

extension UITextView {
    func resetTintColor(){
        let color = tintColor
        tintColor = .clear
        tintColor = color
    }
}

2) in an UITextViewDelegate implementation:

func textViewDidBeginEditing(_ textView: UITextView) {
    textView.resetTintColor()
}
like image 81
dr OX Avatar answered Oct 20 '22 01:10

dr OX


I was able to recreate your behavior: If I change the tint and text color while the text view isn't selected (aka: not first responder), everything will work as expected.

But if I first select it, by tapping it and than change the color by button press, they caret's (tint) color won't change.

Here is a workaround:

- (IBAction)changeColor:(id)sender 
{
    [_textView setTextColor:[UIColor greenColor]];
    [_textView setTintColor:[UIColor greenColor]];

    if([_textView isFirstResponder]){
        [_textView resignFirstResponder];
        [_textView becomeFirstResponder];
    }
}
like image 21
vikingosegundo Avatar answered Oct 19 '22 23:10

vikingosegundo


You can use

[_textView reloadInputViews];

And in Swift

textView. reloadInputViews()

Then you won't need to handle keyboard incoming and going.

like image 41
Nikhil Manapure Avatar answered Oct 20 '22 01:10

Nikhil Manapure