objective-c 更改 UITextField 中光标的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2190352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change color of cursor in UITextField
提问by monish
How can I change the color of the cursor in my UITextField?
如何更改光标的颜色UITextField?
回答by Cap
With iOS 7 you can simply change tintColorproperty of the UITextField. This will affect both the color of the text cursor and the text selection highlight color.
与iOS 7,你可以简单地改变tintColor的财产UITextField。这将影响文本光标的颜色和文本选择突出显示的颜色。
You can do this in code ...
您可以在代码中执行此操作...
textField.tintColor = [UIColor redColor];
...
...
In Swift 4:
在 Swift 4 中:
textField.tintColor = UIColor.red
...or in Interface Builder:
...或在界面生成器中:


You can also do this for all text fields in your app using the UITextFieldappearance proxy:
您还可以使用UITextField外观代理对应用中的所有文本字段执行此操作:
[[UITextField appearance] setTintColor:[UIColor redColor]];
In Swift 4:
在 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.
下面是模拟器屏幕截图,显示了一个普通的 iOS 7 文本字段,其色调设置为红色。
Text cursor screenshot:
文字光标截图:


Text selection screenshot:
文字选择截图:


回答by lemnar
In iOS, UITextfieldhas a textInputTraitsproperty. One of the private properties of UITextInputTraitsis insertionPointColor.
在iOS中,UITextfield有一个textInputTraits属性。的私有属性之一UITextInputTraits是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:
因为它是一个未公开的属性,所以设置自定义颜色可能会使您的应用程序被 App Store 拒绝。如果这不是问题,这应该有效:
[[addNewCategoryTextField textInputTraits] setValue:[UIColor redColor]
forKey:@"insertionPointColor"];
回答by Durgesh
[[self.searchTextField valueForKey:@"textInputTraits"] setValue:[UIColor redColor] forKey:@"insertionPointColor"];
回答by Laurent Etiemble
If you are developing on Mac OS X, then you can try the setInsertionPointColor:method. See NSTextViewreference for more details.
如果您是在 Mac OS X 上开发,那么您可以尝试该setInsertionPointColor:方法。有关更多详细信息,请参阅NSTextView参考。
回答by malex
Durgesh's approach does work.
Durgesh 的方法确实有效。
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.
我也多次使用过这样的 KVC 解决方案。尽管它似乎没有记录,但它确实有效。坦率地说,您在这里不使用任何私有方法 - 只有合法的键值编码。
It is drastically different from [addNewCategoryTextField textInputTraits].
它与 [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.
PS昨天我的新应用程序出现在AppStore上,这种方法没有任何问题。当我使用 KVC 更改某些只读属性(如 navigatonBar)或私有 ivars 时,这并不是第一种情况。
回答by Kamran
To change the cursorcolorthroughout the app for UITextField/UITextView, appearance proxy can also be used as below,
要更改cursorcolor整个应用程序UITextField/UITextView,也可以使用外观代理,如下所示,
UITextField.appearance().tintColor = .green
UITextView.appearance().tintColor = .green

