Change UITextField and UITextView Cursor / Caret Color

前端 未结 16 2030
醉话见心
醉话见心 2020-12-07 09:33

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 f

相关标签:
16条回答
  • 2020-12-07 09:41

    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

    0 讨论(0)
  • 2020-12-07 09:43

    Swift 3:

      UITextField.appearance().tintColor = UIColor.black
      UITextView.appearance().tintColor = UIColor.black
    
    0 讨论(0)
  • 2020-12-07 09:43
    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.

    0 讨论(0)
  • 2020-12-07 09:51

    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.

    0 讨论(0)
  • 2020-12-07 09:54

    For Interface Builder version with Swift

    @IBOutlet weak var tvValue: UITextView! {
            didSet {
                tvValue.tintColor = .black
            }
        }
    
    0 讨论(0)
  • 2020-12-07 09:57

    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.

    0 讨论(0)
提交回复
热议问题