Setting cursor position of UITextField

2019-07-04 06:43发布

问题:

I´ve looked at the monotouch samples and tried:

var range = new NSRange(selectionStart, 1);
NumberTextField.SelectedTextRange = IndexedRange.GetRange(range);

But this causes this exception at runtime: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[iOS_IndexedRange asRange]: unrecognized selector sent to instance 0x7d9bd690

I also looked at a lot of objective-c versions but i cant figure out how to do them on monotouch.

回答1:

Ruben Macias over at the Xamarin forums helped me.

entryField.BecomeFirstResponder();
var indexToSet = 3; //set cursor after the 3rd letter
var positionToSet = entryField.GetPosition(entryField.BeginningOfDocument, indexToSet);
entryField.SelectedTextRange = entryField.GetTextRange(positionToSet, positionToSet);