Change color of cursor in UITextField
Solution 1
With iOS 7 you can simply change tintColor
property of the UITextField
. This will affect both the color of the text cursor and the text selection highlight color.
You can do this in code ...
textField.tintColor = [UIColor redColor];
...
In Swift 4:
textField.tintColor = UIColor.red
...or in Interface Builder:
You can also do this for all text fields in your app using the UITextField
appearance proxy:
[[UITextField appearance] setTintColor:[UIColor redColor]];
In Swift 4:
UITextField.appearance().tintColor = UIColor.red
Below are simulator screenshots showing what an otherwise ordinary iOS 7 text field looks like with its tint set to red.
Text cursor screenshot:
Text selection screenshot:
Solution 2
In iOS, UITextfield
has a textInputTraits
property. One of the private properties of UITextInputTraits
is insertionPointColor
.
Because it's an undocumented property, setting a custom color will probably get your app rejected from the App Store. If that's not a concern, this should work:
[[addNewCategoryTextField textInputTraits] setValue:[UIColor redColor]
forKey:@"insertionPointColor"];
Solution 3
[[self.searchTextField valueForKey:@"textInputTraits"] setValue:[UIColor redColor] forKey:@"insertionPointColor"];
Solution 4
If you are developing on Mac OS X, then you can try the setInsertionPointColor:
method. See NSTextView reference for more details.
Solution 5
Durgesh's approach does work.
I also used such KVC solutions many times. Despite it seems to be undocumented, but it works. Frankly, you don't use any private methods here - only Key-Value Coding which is legal.
It is drastically different from [addNewCategoryTextField textInputTraits].
P.S. Yesterday my new app appeared at AppStore without any problems with this approach. And it is not the first case when I use KVC in changing some read-only properties (like navigatonBar) or private ivars.
monish
Updated on April 24, 2020Comments
-
monish about 4 years
How can I change the color of the cursor in my
UITextField
? -
monish over 14 yearsYes I tried it by using as: [addNewCategoryTextField setInsertionPointColor:[UIColor redColor]]; But Im getting the warning as:UITextField may not responds to -setInsertionPointColor
-
monish over 14 yearsAnd when I used the function as: [addNewCategoryTextField setInsertionPointColor:[NSColor redColor]]; An error is genereted as NSColor not declared use in function.
-
Laurent Etiemble over 14 yearsThat was part of my answer: this message only applies to Mac OS X, not the iPhone. In your question, you have not specified the platform you are developing on. So I have assumed that it was Mac OS X...
-
Rob Keniger over 14 yearsNot if you're using
UITextField
objects you're not. That's iPhone SDK only. -
Craig B over 11 yearsIf it is undocumented (private), then you are almost certain to be rejected by Apple.
-
phatmann over 10 yearsThis works great in iOS 6. In iOS 7 use the tint color. I will let you know if the app gets rejected, but others have used this trick with no issue.
-
Fattie almost 10 yearsStrangely for me it DOES NOT WORK if you set it in interface builder: I suspect the item shown is for the UIView itself, "not" the text field aspects per se.
-
Paresh Mayani almost 10 years@Sathish In fact, it's an answer! would it be worth if he has posted a comment?
-
tbaranes over 9 yearsSame as @Joe Blow, it's working only if you set it programatically. Moreover, it wasn't working on the simulator too, only on a real device. Strange
-
Alain Stulz over 7 yearsMy app was not rejected for using this. However in iOS 10 this solution causes a crash because the
insertionPointColor
trait was removed. Do not use this solution anymore!