El siguiente contenido se aplica a ambos UITextFieldy UITextView.
Información útil
El comienzo del texto del campo de texto:
let startPosition: UITextPosition = textField.beginningOfDocument
El final del texto del campo de texto:
let endPosition: UITextPosition = textField.endOfDocument
El rango seleccionado actualmente:
let selectedRange: UITextRange? = textField.selectedTextRange
Obtener la posición del cursor
if let selectedRange = textField.selectedTextRange {
let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
print("\(cursorPosition)")
}
Establecer la posición del cursor
Para establecer la posición, todos estos métodos en realidad establecen un rango con los mismos valores iniciales y finales.
Al Principio
let newPosition = textField.beginningOfDocument
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
Hasta el final
let newPosition = textField.endOfDocument
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
A una posición a la izquierda de la posición actual del cursor
// 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)
}
}
A una posición arbitraria
Empiece por el principio y mueva 5 caracteres hacia la derecha.
let arbitraryValue: Int = 5
if let newPosition = textField.position(from: textField.beginningOfDocument, offset: arbitraryValue) {
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
}
Relacionado
Seleccionar todo el texto
textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument)
Seleccionar un rango de texto
// 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!)
}
Insertar texto en la posición actual del cursor
textField.insertText("Hello")
Notas
Ver también
textField.endOfDocument. También puede usar guiones bajos o espacios para representar las letras que faltan.en mi caso tuve que usar DispatchQueue:
nada más de este y otros hilos funcionó.
PD: Verifiqué dos veces en qué hilo se estaba ejecutando textViewDidBeginEditing, y era el hilo principal, ya que toda la interfaz de usuario debería ejecutarse, así que no estoy seguro de por qué funcionó ese pequeño retraso con main.asynch.
fuente
textViewDidBeginEditing. Entonces, al colocarlo en la cola, sucederá después de que la persona que llama haya terminado.Para establecer la posición del cursor en su punto:
Para restablecer la posición del cursor:
Nota : Este ejemplo funciona tanto en Textview como en Textfield.
fuente
fuente