How can I change the color of the cursor in my UITextField
?
-
1You need to clarify if you're working with Cocoa or Cocoa touch. You could have made this clear by putting actual class names in your question. – uchuugaka Sep 12 '13 at 09:37
6 Answers
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:
-
8Strangely 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. – Fattie Jul 24 '14 at 16:26
-
1Same 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 – tbaranes Feb 09 '15 at 08:45
-
It is unbelievable to me that this nonsense is being promoted as an actual solution. This just changes the tintcolor of the text field, which if not white, will modify a lot more than just the cursor. Please DO NOT USE THIS. – gran_profaci Jan 26 '23 at 09:43
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"];

- 4,063
- 1
- 32
- 44
-
If it is undocumented (private), then you are almost certain to be rejected by Apple. – Craig B Jan 18 '13 at 21:55
-
1This 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. – phatmann Oct 15 '13 at 01:07
-
2My 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!** – Alain Stulz Sep 26 '16 at 07:59
[[self.searchTextField valueForKey:@"textInputTraits"] setValue:[UIColor redColor] forKey:@"insertionPointColor"];

- 2,888
- 1
- 25
- 36

- 113
- 1
- 1
If you are developing on Mac OS X, then you can try the setInsertionPointColor:
method. See NSTextView reference for more details.

- 27,111
- 5
- 56
- 81
-
Yes I tried it by using as: [addNewCategoryTextField setInsertionPointColor:[UIColor redColor]]; But Im getting the warning as:UITextField may not responds to -setInsertionPointColor – monish Feb 03 '10 at 09:27
-
And when I used the function as: [addNewCategoryTextField setInsertionPointColor:[NSColor redColor]]; An error is genereted as NSColor not declared use in function. – monish Feb 03 '10 at 09:34
-
1That 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... – Laurent Etiemble Feb 03 '10 at 09:53
-
7Not if you're using `UITextField` objects you're not. That's iPhone SDK only. – Rob Keniger Feb 03 '10 at 12:25
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.

- 9,874
- 3
- 56
- 77
-
1@Sathish In fact, it's an answer! would it be worth if he has posted a comment? – Paresh Mayani Aug 27 '14 at 10:26
To change the cursor
color
throughout the app for UITextField/UITextView
, appearance proxy can also be used as below,
UITextField.appearance().tintColor = .green
UITextView.appearance().tintColor = .green

- 14,987
- 4
- 33
- 51