我想知道在iOS中改变UITextField(和UITextView如果答案相同)中的光标/插入符号的颜色。我看到过OSX开发的答案,但没有iOS的答案。

这可能吗?


当前回答

试试,这对我很有用。

[[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor redColor] strong textforKey:@"insertionPointColor"];

其他回答

对于在SwiftUI中搜索Textfield的人来说,这是accentColor:

TextField("Label", text: $self.textToBind).accentColor(Color.red)

斯威夫特4

在viewDidLoad()中只调用下面的代码:

代码示例

//txtVComplaint is a textView

txtVComplaint.tintColor = UIColor.white

txtVComplaint.tintColorDidChange()

一个更通用的方法是设置UIView的外观的tintColor。

UIColor *myColor = [UIColor purpleColor];
[[UIView appearance] setTintColor:myColor];

如果您使用许多默认UI元素,这是有意义的。

试试,这对我很有用。

[[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor redColor] strong textforKey:@"insertionPointColor"];
yourTextField.tintColor = [UIColor whiteColor];

如果你在代码中设置它,它就会工作,因为在Interface Builder (Xcode 6.1.1)中,不知为何颜色触发器不会这样做。它非常适合,不需要更改任何外观代理。