ios 在ios中获取UITextField的光标位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16432364/
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
Getting the cursor position of UITextField in ios
提问by lakesh
I am trying to control the cursor position in the UITextField. Users cannot insert more than one character at a time into the middle of the text field. It moves it to the end of the textfield. So this post in SO: Control cursor position in UITextFieldIt solves my problem. But I need to know the current cursor position.
我正在尝试控制 UITextField 中的光标位置。用户一次不能在文本字段的中间插入一个以上的字符。它将它移动到文本字段的末尾。所以这篇文章在 SO: Control cursor position in UITextField它解决了我的问题。但我需要知道当前的光标位置。
My code looks like this:
我的代码如下所示:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag == 201)
{
[myclass selectTextForInput:textField atRange:NSMakeRange(idx, 0)];
}
}
It is giving me an error at idx. How do I find that?
它在 idx 上给我一个错误。我怎么找到那个?
回答by rmaddy
UITextField
conforms to the UITextInput
protocol which has methods for getting the current selection. But the methods are complicated. You need something like this:
UITextField
符合UITextInput
具有获取当前选择的方法的协议。但是方法很复杂。你需要这样的东西:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.tag == 201) {
UITextRange *selRange = textField.selectedTextRange;
UITextPosition *selStartPos = selRange.start;
NSInteger idx = [textField offsetFromPosition:textField.beginningOfDocument toPosition:selStartPos];
[myclass selectTextForInput:textField atRange:NSMakeRange(idx, 0)];
}
}
回答by Suragch
Swift version
迅捷版
if let selectedRange = textField.selectedTextRange {
let cursorPosition = textField.offsetFromPosition(textField.beginningOfDocument, toPosition: selectedRange.start)
print("\(cursorPosition)")
}
My full answer about getting and setting the cursor position is here.
我关于获取和设置光标位置的完整答案在这里。
回答by Dimitar K
The code you have posted won't work for determining where the cursor is. You need the get method, not the set. It should be something along the lines of:
您发布的代码无法用于确定光标所在的位置。您需要 get 方法,而不是 set。它应该是这样的:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag == 201)
{
UITextRange selectedRange = [textField selectedTextRange];
// here you will have to check whether the user has actually selected something
if (selectedRange.empty) {
// Cursor is at selectedRange.start
...
} else {
// You have not specified home to handle the situation where the user has selected some text, but you can use the selected range and the textField selectionAffinity to assume cursor is on the left edge of the selected range or the other
...
}
}
}
For more information - check the UITextInput protocol http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextInput_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITextInput
有关更多信息 - 检查 UITextInput 协议http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextInput_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITextInput
Update: @rmaddy has posted some good extra bits I missed in my response - how to handle the text position from the NSTextRange and convert NSTextPosition to int.
更新:@rmaddy 已经发布了一些我在回复中遗漏的很好的额外位 - 如何处理来自 NSTextRange 的文本位置并将 NSTextPosition 转换为 int。
回答by Vision Mkhabela
Swift Solution.
快速解决方案。
You can set the cursor padding by subclassing the UITextfield class and implementing these two methods.
您可以通过继承 UITextfield 类并实现这两个方法来设置光标填充。
Hope it helps someone in need.
希望能帮到有需要的人。
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: UIEdgeInsets.init(top: 4, left: 40, bottom: 1, right: 15))
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: UIEdgeInsets.init(top: 4, left: 40, bottom: 1, right: 15))
}