代码之家  ›  专栏  ›  技术社区  ›  Jeremy

UITextField中的控制光标位置

  •  47
  • Jeremy  · 技术社区  · 15 年前

    我有一个 UITextField 如果用户将光标移到字符串结尾以外的某个地方,则我的格式更改会将光标移到字符串结尾。这意味着用户一次不能在文本字段的中间插入多个字符。 有没有办法记住并重置 输入框 ?

    7 回复  |  直到 10 年前
        1
  •  68
  •   Chris R    12 年前

    控制UITextField中的光标位置非常复杂,因为输入框和计算位置涉及到太多抽象。然而,这肯定是有可能的。您可以使用成员函数 setSelectedTextRange

    [input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
    

    这里有一个函数,它获取一个范围并选择该范围内的文本。如果只想将光标放在某个索引处,只需使用长度为0的范围:

    + (void)selectTextForInput:(UITextField *)input atRange:(NSRange)range {
        UITextPosition *start = [input positionFromPosition:[input beginningOfDocument] 
                                                     offset:range.location];
        UITextPosition *end = [input positionFromPosition:start
                                                   offset:range.length];
        [input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
    }
    

    例如,将光标放置在 idx input :

        [Helpers selectTextForInput:input 
                            atRange:NSMakeRange(idx, 0)];
    
        2
  •  4
  •   krlzlx    8 年前

    有助于在索引处定位(Swift 3)

    private func setCursorPosition(input: UITextField, position: Int) {
        let position = input.position(from: input.beginningOfDocument, offset: position)!
        input.selectedTextRange = input.textRange(from: position, to: position)
    }
    
        3
  •  3
  •   Dan J Akhil    9 年前

    我终于找到了解决这个问题的办法!您可以将需要插入的文本放入系统粘贴板,然后将其粘贴到当前光标位置:

    [myTextField paste:self]  
    


    http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html

    粘贴功能是OSV3.0特有的,但我已经测试过了,它可以很好地使用自定义键盘。

        4
  •  2
  •   gabuchan    8 年前

    这是@Chris R的Swift版本。 为Swift3更新

    private func selectTextForInput(input: UITextField, range: NSRange) {
        let start: UITextPosition = input.position(from: input.beginningOfDocument, offset: range.location)!
        let end: UITextPosition = input.position(from: start, offset: range.length)!
        input.selectedTextRange = input.textRange(from: start, to: end)
    }
    
        5
  •  1
  •   Pavel Alexeev    8 年前

    随便用这个 UITextField 要获取和设置光标位置的类别:

    @interface UITextField (CursorPosition)
    
    @property (nonatomic) NSInteger cursorPosition;
    
    @end
    

    @implementation UITextField (CursorPosition)
    
    - (NSInteger)cursorPosition
    {
        UITextRange *selectedRange = self.selectedTextRange;
        UITextPosition *textPosition = selectedRange.start;
        return [self offsetFromPosition:self.beginningOfDocument toPosition:textPosition];
    }
    
    - (void)setCursorPosition:(NSInteger)position
    {
        UITextPosition *textPosition = [self positionFromPosition:self.beginningOfDocument offset:position];
        [self setSelectedTextRange:[self textRangeFromPosition:textPosition toPosition:textPosition]];
    }
    
    @end
    
        6
  •  0
  •   pix0r    15 年前

    我不认为有一种方法可以将光标放在你的电脑中的某个特定位置 UITextField (除非你非常狡猾,并且模拟了一个触摸事件)。相反,我会在用户需要时处理格式化 完成 编辑他们的文本(在 textFieldShouldEndEditing:

        7
  •  -2
  •   Community CDub    8 年前

    下面是一个可以很好地解决此问题的代码片段:

    - (void)textFieldDidBeginEditing:(UITextField *)textField{
        UITextPosition *positionBeginning = [textField beginningOfDocument];
        UITextRange *textRange =[textField textRangeFromPosition:positionBeginning 
                                                      toPosition:positionBeginning];
        [textField setSelectedTextRange:textRange];
    }
    

    Source from @omz

    推荐文章