xcode uitextfield 中的光标位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2778703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cursor location in a uitextfield
提问by Allen
im using customkeyboard in my controller. but how to get the cursor location in the uitextfiled. my requirement is to enter a charecter in a desired location of textfield. is it possible?
我在我的控制器中使用 customkeyboard。但是如何在 uitextfiled 中获取光标位置。我的要求是在文本字段的所需位置输入一个字符。是否可以?
回答by idz
Let's assume that you code is in a method on an object where self.textField
is the UITextField in question.
让我们假设您的代码位于对象的方法中,其中self.textField
UITextField 是有问题的。
You can find the current position of the cursor/selection with:
您可以使用以下命令找到光标/选择的当前位置:
NSRange range = self.textField.selectedTextRange;
If the user has not selected text the range.length
will be 0, indicating that it is just a cursor. Unfortunately this property does not appear to be KVO compliant, so there is no efficient way to be informed when it changes, however this should not be a problem in your case because you probably really only care about it when you are responding to user interaction with your custom keyboard.
如果用户没有选择文本range.length
,则为 0,表示它只是一个光标。不幸的是,此属性似乎不符合 KVO,因此没有有效的方法可以在更改时通知它,但是在您的情况下这应该不是问题,因为您可能真的只在响应用户交互时才关心它您的自定义键盘。
You can then use (assuming newText
holds the input from your custom keyboard).
然后您可以使用(假设newText
保留来自您的自定义键盘的输入)。
[self.textField replaceRange:range withText:newText];
If you need to subsequently adjust the cursor/selection you can use:
如果您需要随后调整光标/选择,您可以使用:
self.textField.selectedTextRange = newRange;
For example, you may want to position the cursor after the text you inserted.
例如,您可能希望将光标定位在您插入的文本之后。
UPDATE:
更新:
In my original answer I failed to notice that I was leveraging a category I had added to UITextView
:
在我最初的回答中,我没有注意到我正在利用我添加的一个类别UITextView
:
- (void)setSelectedRange:(NSRange)selectedRange
{
UITextPosition* from = [self positionFromPosition:self.beginningOfDocument offset:selectedRange.location];
UITextPosition* to = [self positionFromPosition:from offset:selectedRange.length];
self.selectedTextRange = [self textRangeFromPosition:from toPosition:to];
}
- (NSRange)selectedRange
{
UITextRange* range = self.selectedTextRange;
NSInteger location = [self offsetFromPosition:self.beginningOfDocument toPosition:range.start];
NSInteger length = [self offsetFromPosition:range.start toPosition:range.end];
NSAssert(location >= 0, @"Location is valid.");
NSAssert(length >= 0, @"Length is valid.");
return NSMakeRange(location, length);
}
Then replace use self.textField.selectedRange
instead of self.textField.selectedTextRange
and proceed as I described.
然后替换 useself.textField.selectedRange
而不是self.textField.selectedTextRange
并按照我的描述继续。
Thanks to omz for pointing out my error.
感谢 omz 指出我的错误。
Of course, you can work directly with UITextRange
but, at least in my case, this proved to be rather ungainly.
当然,您可以直接使用,UITextRange
但至少在我的情况下,这被证明是相当笨拙的。
回答by Suragch
Swift
迅速
Get the cursor location:
获取光标位置:
if let selectedRange = textField.selectedTextRange {
let cursorPosition = textField.offsetFromPosition(textField.beginningOfDocument, toPosition: selectedRange.start)
}
Enter text at some arbitrary location:
在任意位置输入文本:
let arbitraryValue: Int = 5
if let newPosition = textField.positionFromPosition(textField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: arbitraryValue) {
textField.selectedTextRange = textField.textRangeFromPosition(newPosition, toPosition: newPosition)
textField.insertText("Hello")
}
My full answer is here.
我的完整答案在这里。
回答by Henrik Erlandsson
The answer is that you can't get the current cursor location for all types of editing that can be done with the textfield. You can insert characters at the cursor with [textField paste], but the user can move the cursor, select and modify text, without a way to get notified where the cursor ended up.
答案是对于可以使用文本字段完成的所有类型的编辑,您无法获得当前光标位置。您可以使用 [textField paste] 在光标处插入字符,但用户可以移动光标、选择和修改文本,而无法获得光标结束位置的通知。
You can temporarily paste a special character and search its position in the string, remove it, and then add the character you want to have there.
您可以临时粘贴一个特殊字符并搜索它在字符串中的位置,将其删除,然后在那里添加您想要的字符。