Change Uitextfield and Uitextview Cursor/Caret Color

Change UITextField and UITextView Cursor / Caret Color

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:

Swift 3.0:

UITextField.appearance().tintColor = .black 

Objective-C:

[[UITextField appearance] setTintColor:[UIColor blackColor]];

Same answer applies for an individual UITextField:

Swift 3.0:

myTextField.tintColor = .black 

Objective-C

[myTextField setTintColor:[UIColor blackColor]];

How To Change UITextView Cursor Blink Color in Xcode

just change Tint Color

UITextView.appearance().tintColor = .black 

Objective-C:

[[UITextView appearance] setTintColor:[UIColor blackColor]];

Change prompt color for UITextField or UITextView on Mac Catalyst

After a lot of searching around, I think I've found a workaround for this. You can use the key-value coding paradigms to get at the insertionPointColor property (which is what you ultimately need to set). Here is an example of setting the caret color to red. Be careful to only do this when targeting Mac Catalyst, as using tintColor on iOS is proper.

#if targetEnvironment(macCatalyst)
let textInputTraits = myTextView.value(forKey: "textInputTraits") as? NSObject
textInputTraits?.setValue(UIColor.red, forKey: "insertionPointColor")
#endif

As of 2022 this works perfectly for both UITextField and UITextView on Mac Catalyst builds running on a Mac.

UITextView Cursor Color not changing iOS 7

I was able to recreate your behavior: If I change the tint and text color while the text view isn't selected (aka: not first responder), everything will work as expected.

But if I first select it, by tapping it and than change the color by button press, they caret's (tint) color won't change.

Here is a workaround:

- (IBAction)changeColor:(id)sender 
{
[_textView setTextColor:[UIColor greenColor]];
[_textView setTintColor:[UIColor greenColor]];

if([_textView isFirstResponder]){
[_textView resignFirstResponder];
[_textView becomeFirstResponder];
}
}

How do I set the caret cursor color on an MDCTextField?

Thanks for using MDC-iOS.

Cursor color has just been added as a parameter on MDCTextField (.cursorColor).

It was included in the release 38.1.0.

Getting and Setting Cursor Position of UITextField and UITextView in Swift

The following content applies to both UITextField and UITextView.

Useful information

The very beginning of the text field text:

let startPosition: UITextPosition = textField.beginningOfDocument

The very end of the text field text:

let endPosition: UITextPosition = textField.endOfDocument

The currently selected range:

let selectedRange: UITextRange? = textField.selectedTextRange

Get cursor position

if let selectedRange = textField.selectedTextRange {

let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)

print("\(cursorPosition)")
}

Set cursor position

In order to set the position, all of these methods are actually setting a range with the same start and end values.

To the beginning

let newPosition = textField.beginningOfDocument
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)

To the end

let newPosition = textField.endOfDocument
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)

To one position to the left of the current cursor position

// only if there is a currently selected range
if let selectedRange = textField.selectedTextRange {

// and only if the new position is valid
if let newPosition = textField.position(from: selectedRange.start, offset: -1) {

// set the new position
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
}
}

To an arbitrary position

Start at the beginning and move 5 characters to the right.

let arbitraryValue: Int = 5
if let newPosition = textField.position(from: textField.beginningOfDocument, offset: arbitraryValue) {

textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
}

Related

Select all text

textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)

Select a range of text

// Range: 3 to 7
let startPosition = textField.position(from: textField.beginningOfDocument, offset: 3)
let endPosition = textField.position(from: textField.beginningOfDocument, offset: 7)

if startPosition != nil && endPosition != nil {
textField.selectedTextRange = textField.textRange(from: startPosition!, to: endPosition!)
}

Insert text at the current cursor position

textField.insertText("Hello")

Notes

  • Use textField.becomeFirstResponder() to give focus to the text field and make the keyboard appear.

  • See this answer for how to get the text at some range.

See also

  • How to Create a Range in Swift


Related Topics



Leave a reply



Submit