我一直在试验UITextField,以及如何使用它的光标位置.我已经找到了一些关系Objective-C的答案,如下所示

但由于我与SWIFT合作,我想了解如何获取当前光标位置并在SWIFT中进行设置.

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

推荐答案

以下内容同时适用于UITextFieldUITextView.

Useful information

文本字段文本的最开始位置:

let startPosition: UITextPosition = textField.beginningOfDocument

文本字段文本的最末尾:

let endPosition: UITextPosition = textField.endOfDocument

当前选定的范围:

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

为了设置位置,所有这些方法实际上都是使用相同的起始值和结束值设置范围.

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

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

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

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

  • 有关如何在某个范围内获取文本的信息,请参见this answer.

See also

Ios相关问答推荐

Flutter WebRTC:GetUserMedia()不适用于Safari和IOS设备

实例方法wait在异步上下文中不可用;请改用TaskGroup;这是Swift 6中的错误''

Xcode版本15.2上未显示iOS 16.4的模拟器

创建带有Flutter 翼的会所头像

当包含UITextView的inputAccessoryView显示键盘时,iOS UITableView内容会出现UINavigationBar奇怪的错误

如何在wiftUI中管理导航栈?

为什么iOS委派在KMM中不能正常工作?

@Query与SwiftData数据列表绑定

Flutter Aging Person 自定义小部件

在 Swift 中使用 UITesting 捕获屏幕录像

UICollectionView - 水平滚动,水平布局?

UILabel - 字符串作为文本和链接

在主线程上发布 NSNotification

如何比较两个 UIImage 对象

iOS 10 错误:UICollectionView 接收到具有不存在索引路径的单元格的布局属性

什么是伞头?

使用 Chrome DevTools 调试 iOS 6+7 Mobile Safari

NSURLSession:如何增加 URL 请求的超时时间?

我可以通过 UIAppearance 代理设置哪些属性?

ARC - __unsafe_unretained 的含义?