Change color of cursor in UITextField

42,383

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:

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

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.

Share:
42,383
monish
Author by

monish

Updated on April 24, 2020

Comments

  • monish
    monish about 4 years

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

  • monish
    monish over 14 years
    Yes I tried it by using as: [addNewCategoryTextField setInsertionPointColor:[UIColor redColor]]; But Im getting the warning as:UITextField may not responds to -setInsertionPointColor
  • monish
    monish over 14 years
    And when I used the function as: [addNewCategoryTextField setInsertionPointColor:[NSColor redColor]]; An error is genereted as NSColor not declared use in function.
  • Laurent Etiemble
    Laurent Etiemble over 14 years
    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...
  • Rob Keniger
    Rob Keniger over 14 years
    Not if you're using UITextField objects you're not. That's iPhone SDK only.
  • Craig B
    Craig B over 11 years
    If it is undocumented (private), then you are almost certain to be rejected by Apple.
  • phatmann
    phatmann over 10 years
    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.
  • Fattie
    Fattie almost 10 years
    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.
  • Paresh Mayani
    Paresh Mayani almost 10 years
    @Sathish In fact, it's an answer! would it be worth if he has posted a comment?
  • tbaranes
    tbaranes over 9 years
    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
  • Alain Stulz
    Alain Stulz over 7 years
    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!