在Swift中获取和设置UITextField和UITextView的光标位置 [英] Getting and Setting Cursor Position of UITextField and UITextView in Swift
问题描述
我一直在试验 UITextField
以及如何使用它的光标位置。我发现了一些关系Objective-C的答案,如
<但是,由于我正在使用Swift,我想学习如何获取当前光标位置并在Swift中设置它。
以下答案是结果来自Objective-C的实验和翻译。
此答案已针对Swift 3进行了更新
以下内容适用于 UITextField
和 U ITextView
。
有用信息
文本字段文本的开头:
让startPosition:UITextPosition = textField.beginningOfDocument
文本字段文本的最后:
let endPosition:UITextPosition = textField .endOfDocument
当前选择的范围:
让selectedRange:UITextRange? = textField.selectedTextRange
获取光标位置
如果let selectedRange = textField.selectedTextRange {
let cursorPosition = textField.offset(from:textField.beginningOfDocument,to:selectedRange.start)
print(\(cursorPosition))
}
设置光标位置
为了设置位置,所有这些方法实际上都设置了一个具有相同开始和结束值的范围。
从头开始
让newPosition = textField.beginningOfDocument
textField。 selectedTextRange = textField.textRange(from:newPosition,to:newPosition)
到最后
让newPosition = textField.endOfDocument
textField.selectedTextRange = textField.textRange(from:newPosition,to :newPosition)
当前光标位置左侧的一个位置
//仅当当前选中时范围
如果让selectedRange = textField.selectedTextRange {
//并且仅当新位置有效时才
如果让newPosition = textField.position(来自:selectedRange.start,offset) :-1){
//设置新位置
textField.selectedTextRange = textField.textRange(from:newPosition,to:newPosition)
}
}
到任意位置
从头开始向右移动5个字符。
let arbitraryValue:Int = 5
如果让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)
选择一系列文字
//范围:3到7
让startPosition = textField.position(from:textField.beginningOfDocument,offset:3)
让endPosition = textField.position(from:textField.beginningOfDocument,offset:7)
如果startPosition!= nil&& endPosition!= nil {
textField.selectedTextRange = textField.textRange(from:startPosition!,to:endPosition!)
}
在当前光标位置插入文字
textField .insertText(你好)
注释
-
使用
textField.becomeFirstResponder()
将焦点放在文本字段上并使键盘显示。 -
请参阅此答案,了解如何在某个范围内获取文字。
参见
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
- Getting the cursor position of UITextField in ios
- Control cursor position in UITextField
- UITextField get currently edited word
But since I am working with Swift, I wanted to learn how to get the current cursor location and also set it in Swift.
The answer below is the the result of my experimentation and translation from Objective-C.
This answer has been updated for Swift 3
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
这篇关于在Swift中获取和设置UITextField和UITextView的光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!