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

这可能吗?


当前回答

这在swift中很管用:

UITextField.tintColor = UIColor.blackColor()

你也可以在storyboard中设置:https://stackoverflow.com/a/18759577/3075340

其他回答

斯威夫特3:

  UITextField.appearance().tintColor = UIColor.black
  UITextView.appearance().tintColor = UIColor.black

斯威夫特4

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

代码示例

//txtVComplaint is a textView

txtVComplaint.tintColor = UIColor.white

txtVComplaint.tintColorDidChange()

试试,这对我很有用。

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

接口生成器版本与Swift

@IBOutlet weak var tvValue: UITextView! {
        didSet {
            tvValue.tintColor = .black
        }
    }

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

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