开发者

Cursor location in a uitextfield

im using customkeybo开发者_开发百科ard 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?


Let's assume that you code is in a method on an object where self.textField is the UITextField in question.

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.

You can then use (assuming newText holds the input from your custom keyboard).

[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:

- (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.

Thanks to omz for pointing out my error.

Of course, you can work directly with UITextRange but, at least in my case, this proved to be rather ungainly.


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.

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜