在 Swift 中获取和设置 UITextField 和 UITextView 的光标位置 [英] Getting and Setting Cursor Position of UITextField and UITextView in Swift

查看:113
本文介绍了在 Swift 中获取和设置 UITextField 和 UITextView 的光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验 UITextField 以及如何处理它的光标位置.我发现了许多关系 Objective-C 答案,如

I've been experimenting with UITextField and how to work with it's cursor position. I've found a number of relation Objective-C answers, as in

但由于我使用 Swift,我想学习如何获取当前光标位置并在 Swift 中设置它.

But since I am working with Swift, I wanted to learn how to get the current cursor location and also set it in Swift.

下面的答案是我从Objective-C实验和翻译的结果.

The answer below is the the result of my experimentation and translation from Objective-C.

推荐答案

以下内容同时适用于UITextFieldUITextView.

The following content applies to both UITextField and UITextView.

文本字段文本的最开始:

The very beginning of the text field text:

let startPosition: UITextPosition = textField.beginningOfDocument

文本字段文本的最后:

let endPosition: UITextPosition = textField.endOfDocument

当前选择的范围:

let selectedRange: UITextRange? = textField.selectedTextRange

获取光标位置

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.

开始

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

到最后

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

到当前光标位置左侧一个位置

// 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)
    }
}

到任意位置

从头开始向右移动 5 个字符.

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)
}

相关

选择所有文本

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

选择文本范围

// 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!)
}

在当前光标位置插入文本

textField.insertText("Hello")

注意事项

  • 使用 textField.becomeFirstResponder() 将焦点放在文本字段上并使键盘出现.

    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.

      这篇关于在 Swift 中获取和设置 UITextField 和 UITextView 的光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆