ios 更改 UITextField 和 UITextView 光标/插入符号颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11606007/
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 UITextField and UITextView Cursor / Caret Color
提问by RileyE
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.
我想知道如何在 iOS 中更改 a UITextField
(UITextView
如果答案相同)中光标/插入符号的颜色。我已经看到了 OSX 开发的答案,但没有看到 iOS 的答案。
Is this even possible?
这甚至可能吗?
回答by DiscDev
If you're targeting iOS 7+, this has been made much easier. Simply change the tintColor
of the field with a cursor using the appearance proxy and it will apply throughout the app:
如果您的目标是 iOS 7+,这将变得更加容易。只需tintColor
使用外观代理使用光标更改字段的 ,它将在整个应用程序中应用:
Swift 3.0:
斯威夫特 3.0:
UITextField.appearance().tintColor = .black
Objective-C:
目标-C:
[[UITextField appearance] setTintColor:[UIColor blackColor]];
Same answer applies for an individual UITextField
:
同样的答案适用于个人UITextField
:
Swift 3.0:
斯威夫特 3.0:
myTextField.tintColor = .black
Objective-C
目标-C
[myTextField setTintColor:[UIColor blackColor]];
回答by Cap
With iOS7 you can simply change tintColor of the textField
使用 iOS7,您可以简单地更改 textField 的 tintColor
回答by Peter Kreinz
Swift 3:
斯威夫特 3:
UITextField.appearance().tintColor = UIColor.black
UITextView.appearance().tintColor = UIColor.black
回答by Dov
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.
注意:此答案已过时,应仅用于 iOS 7 之前的开发。请参阅使用 iOS 7 中的外观代理的 1 行解决方案的其他答案。
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.
我设法创建了一个将被 AppStore 审核团队接受的解决方案,因为它不使用任何现有的私有 API。
I have created a control called DGTextField that extends UITextField.
我创建了一个名为 DGTextField 的控件,它扩展了 UITextField。
回答by David
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.
如果您在代码中设置它,它就可以工作,'因为颜色触发器在界面生成器(Xcode 6.1.1)中不会这样做。它非常适合,无需更改任何外观代理。
回答by ilya
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.
设置tintColor
为UITextField
和UITextView
工作方式不同。虽然UITextField
您不需要在更新后调用额外的代码tintColor
来更改光标颜色,但UITextView
您需要。
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).
因此,设置后tintColor
的UITextView
(这并不重要IB或代码),你需要调用textView.tintColorDidChange()
,以应用它(实际上它会通过文本视图的配置到其子视图层次)。
回答by Micro
This worked for me in swift:
这很快对我有用:
UITextField.tintColor = UIColor.blackColor()
You can also set this in storyboard: https://stackoverflow.com/a/18759577/3075340
您也可以在情节提要中进行设置:https: //stackoverflow.com/a/18759577/3075340
回答by arnekolja
A more general approach would be to set the UIView's appearance's tintColor.
更通用的方法是设置 UIView 外观的 tintColor。
UIColor *myColor = [UIColor purpleColor];
[[UIView appearance] setTintColor:myColor];
Makes sense if you're using many default UI elements.
如果您使用许多默认 UI 元素,这很有意义。
回答by Durgesh
Try, Its working for me.
试试吧,它对我有用。
[[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor redColor] strong textforKey:@"insertionPointColor"];
回答by Max Ballo
It is only possible by accessing a private property and therefore may cause an Apple AppStore app rejection.
只有通过访问私有财产才有可能,因此可能会导致 Apple AppStore 应用程序拒绝。