Rod*_*igo 5 iphone objective-c uitextfield ios
我发现了一件事.如果我设置肯定功能
- textField:shouldChangeCharactersInRange:replacementString:
Run Code Online (Sandbox Code Playgroud)
光标停留在我输入的相同位置,如("|"=光标)
"|" => Empty , type a
"a|" => type b
"ab|" => change cursor
"a|b" => type c
"ac|b" => type d
"acd|b" => change cursor
"ac|db" => type backspace
"a|db" => type backspace again
"|db" => *result*
Run Code Online (Sandbox Code Playgroud)
但是,如果我必须更改文本,比如放入"-"倒数第二个字符,我会做逻辑事情并设置文本:
[textField setText:newText];
Run Code Online (Sandbox Code Playgroud)
但上面的示例将采用以下方式:
"|" => Empty , type a
"a|" => type b (put character "-" automatic)
"a-b|" => user change cursor
"a|-b" => type c
"ac-b|" => type d ## the cursor is in end ##
"acb-d|" => change cursor ## "d" is in wrong place, the user have to
## change cursor to reproduce the result above
"ac|b-d" => type backspace
"ab-d|" => type backspace again ## the cursor is in end again ##
"a-b|" => *result* ## I want delete "a" but it delete "d"
Run Code Online (Sandbox Code Playgroud)
如何将光标设置为结果"|d-b"与第一个样本中的相同?或者更好的问题:如何更改UITextField中的文本和光标?
我找到了解决方案。我无法选择光标位置,但它可以满足我的要求。你必须使用UIKeyInput协议的功能有 2 个很好的功能:
- (void)insertText:(NSString *)text;
- (void)deleteBackward;
Run Code Online (Sandbox Code Playgroud)
然后,重做我的测试用例,我有:
"|" => Empty , type a
"a|" => type b (put character "-" automatic)
"a-b|" => user change cursor
"a|-b" => type c (enter the character with a [textField insertText:@"c"]
"ac|-b" => type d (the same thing: [textField insertText:@"d"])
"acd|-b" => change cursor
"ac|d-b" => type backspace (use a [textField deleteBackward])
"a|d-b" => type backspace
"|d-b" => *result* (Work!!!)
Run Code Online (Sandbox Code Playgroud)
您无法更改光标,但对于正常打字来说它可以解决问题!
| 归档时间: |
|
| 查看次数: |
2758 次 |
| 最近记录: |