40

How can I change the color of the cursor in my UITextField?

Willeke
  • 12,344
  • 3
  • 16
  • 41
monish
  • 433
  • 1
  • 4
  • 6
  • 1
    You 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 Answers6

96

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:

screenshot showing how to modify tint of a text field 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 cursor screenshot

Text selection screenshot:

Text selection screenshot

Marcy
  • 1,974
  • 2
  • 19
  • 31
Cap
  • 1,694
  • 13
  • 14
  • 8
    Strangely 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
  • 1
    Same 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
14

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"];
lemnar
  • 4,013
  • 1
  • 30
  • 44
  • If it is undocumented (private), then you are almost certain to be rejected by Apple. – Craig B Jan 18 '13 at 21:55
  • 1
    This 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
  • 2
    My 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
6
[[self.searchTextField valueForKey:@"textInputTraits"] setValue:[UIColor redColor] forKey:@"insertionPointColor"];
Cody Guldner
  • 2,864
  • 1
  • 21
  • 35
Durgesh
  • 113
  • 1
  • 1
3

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

Laurent Etiemble
  • 25,779
  • 5
  • 52
  • 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
  • 1
    That 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
  • 7
    Not if you're using `UITextField` objects you're not. That's iPhone SDK only. – Rob Keniger Feb 03 '10 at 12:25
2

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.

malex
  • 9,374
  • 3
  • 53
  • 71
0

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
Kamran
  • 13,636
  • 3
  • 26
  • 42