Change UITextField and UITextView Cursor / Caret Color

前端 未结 16 2031
醉话见心
醉话见心 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:58

    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

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

    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).

    0 讨论(0)
  • 2020-12-07 10:02

    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.

    0 讨论(0)
  • 2020-12-07 10:02

    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
    
    0 讨论(0)
提交回复
热议问题