Change UITextField and UITextView Cursor / Caret C

2019-01-16 03:09发布

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?

13条回答
淡お忘
2楼-- · 2019-01-16 03:20
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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-16 03:23

With iOS7 you can simply change tintColor of the textField

查看更多
不美不萌又怎样
4楼-- · 2019-01-16 03:24

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

查看更多
forever°为你锁心
5楼-- · 2019-01-16 03:32

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

查看更多
聊天终结者
6楼-- · 2019-01-16 03:33

Swift 3:

  UITextField.appearance().tintColor = UIColor.black
  UITextView.appearance().tintColor = UIColor.black
查看更多
Lonely孤独者°
7楼-- · 2019-01-16 03:36

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.

查看更多
登录 后发表回答