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...
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()
}
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];
}
}
You can use
[_textView reloadInputViews];
And in Swift
textView. reloadInputViews()
Then you won't need to handle keyboard incoming and going.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With