[ios] Change UITextField and UITextView Cursor / Caret Color

I'm wondering about changing the color of the cursor / caret in a UITextField (And UITextView if its the same answer) in iOS. I've seen answers for OSX development, but nothing for iOS.

Is this even possible?

This question is related to ios colors uitextfield cursor uitextview

The answer is


It is only possible by accessing a private property and therefore may cause an Apple AppStore app rejection.

take a look at this Stackoverflow question


Try, Its working for me.

[[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor redColor] strong textforKey:@"insertionPointColor"];

I think If you want some custom colors you can go to Assets.xcassets folder, right click and select New Color Set, once you created you color you set, give it a name to reuse it.

And you can use it just like this :

import UIKit 

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        UITextField.appearance().tintColor = UIColor(named: "YOUR-COLOR-NAME")  #here
    }
}

Tested on macOS 10.15 / iOS 13 / Swift 5


Setting tintColor for UITextField and UITextView works differently. While for UITextField you don't need to call additional code after updating tintColor to change cursor color, but for UITextView you need.

So after setting tintColor for UITextView (it doesn't matter in IB or in code) you need to call textView.tintColorDidChange() in order to apply it (actually it will pass text view's config down to its subviews hierarchy).


For people searching the equivalent in SwiftUI for Textfield this is accentColor:

TextField("Label", text: $self.textToBind).accentColor(Color.red)

Swift 3:

  UITextField.appearance().tintColor = UIColor.black
  UITextView.appearance().tintColor = UIColor.black

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.

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.


yourTextField.tintColor = [UIColor whiteColor];

It works if you set it in code, 'cos somehow color trigger doesn't do it in the Interface Builder (Xcode 6.1.1). It suited well without a need to change any appearance proxy.


If the UITextField is from UISearchBar then first get the textField from searchBar and then apply tintColor property:

let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.tintColor = UIColor.lightGray

With iOS7 you can simply change tintColor of the textField


This worked for me in swift:

UITextField.tintColor = UIColor.blackColor()

You can also set this in storyboard: https://stackoverflow.com/a/18759577/3075340


Swift 4

In viewDidLoad() just call below code:

CODE SAMPLE

//txtVComplaint is a textView

txtVComplaint.tintColor = UIColor.white

txtVComplaint.tintColorDidChange()

Note: This answer is out of date and should be used for pre-iOS 7 development only. See other answers for a 1 line solution using the appearance proxy in iOS 7.

I arrived at this question after I faced the same problem in a project I was working on.

I managed to create a solution that will be accepted by the AppStore review team as it does not use any existing Private APIs.

I have created a control called DGTextField that extends UITextField.


A more general approach would be to set the UIView's appearance's tintColor.

UIColor *myColor = [UIColor purpleColor];
[[UIView appearance] setTintColor:myColor];

Makes sense if you're using many default UI elements.


For Interface Builder version with Swift

@IBOutlet weak var tvValue: UITextView! {
        didSet {
            tvValue.tintColor = .black
        }
    }

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to colors

is it possible to add colors to python output? How do I use hexadecimal color strings in Flutter? How do I change the font color in an html table? How do I print colored output with Python 3? Change bar plot colour in geom_bar with ggplot2 in r How can I color a UIImage in Swift? How to change text color and console color in code::blocks? Android lollipop change navigation bar color How to change status bar color to match app in Lollipop? [Android] How to change color of the back arrow in the new material theme?

Examples related to uitextfield

Keep placeholder text in UITextField on input in IOS Set the maximum character length of a UITextField in Swift How to restrict UITextField to take only numbers in Swift? Disabling user input for UITextfield in swift How do I check when a UITextField changes? Swift add icon/image in UITextField Move textfield when keyboard appears swift Create space at the beginning of a UITextField Max length UITextField How to hide keyboard in swift on pressing return key?

Examples related to cursor

Get all dates between two dates in SQL Server Using external images for CSS custom cursors cursor.fetchall() vs list(cursor) in Python Get current cursor position in a textbox INSERT and UPDATE a record using cursors in oracle Change UITextField and UITextView Cursor / Caret Color How to get the focused element with jQuery? Bold black cursor in Eclipse deletes code, and I don't know how to get rid of it What's the best way to iterate an Android Cursor? SQL Server: how to add new identity column and populate column with ids?

Examples related to uitextview

how to make UITextView height dynamic according to text length? Swift: Display HTML data in a label or textView Add placeholder text inside UITextView in Swift? How can I make a clickable link in an NSAttributedString? UITextView that expands to text using auto layout Change UITextField and UITextView Cursor / Caret Color Display html text in uitextview How to style UITextview to like Rounded Rect text field? How to create a multiline UITextfield? Placeholder in UITextView