Независимо от того, что я делаю, я НЕ могу установить курсор в позицию 1 в этой строке (___) ___-____
позиция 1 - это позиция, следующая сразу за открытием (
. Я делаю это в методе EditingStarted
делегата. В качестве руководства я следую коду здесь . в настоящее время мой код:
public override void EditingStarted(UITextField textField)
{
if (MyParent.EditMask != "")
{
textField.Text = MyParent.EditMask.Replace("#", "_");
// Set cursor position
NSRange therange = new NSRange(index, 0);
UITextPosition start = textField.GetPosition(textField.BeginningOfDocument, therange.Length - 1);
UITextPosition end = textField.GetPosition(start, therange.Length);
textField.SelectedTextRange = textField.GetTextRange(start, end);
}
}
Курсор ВСЕГДА появляется сразу после закрывающего )
, ничего из того, что я делаю, не меняет. Понятия не имею почему. Я пробовал получить позицию первого подчеркивания:
int position = textField.Text.IndexOf("_");
NSRange therange = new NSRange(position, 0);
Но опять же, это приводит к тому, что курсор позиционируется сразу после закрытия )
. Кто-нибудь видит, что я делаю неправильно?
**** Обновление ****
Просто, чтобы люди понимали контекст, приведенный выше код является частью класса с именем UIMaskedTextField
, который Я создал для обработки всей моей маски / ввода текста в пользовательском интерфейсе. Этот класс:
class UIMaskedTextField : UITextField
{
public string EditMask { get; set; }
public UIMaskedTextField()
{
this.Delegate = new MaskTextViewDelegate(this);
}
}
class MaskTextViewDelegate : UITextFieldDelegate
{
private UIMaskedTextField MyParent;
int index = 0;
public MaskTextViewDelegate(UIMaskedTextField parent)
{
MyParent = parent;
}
public override void EditingStarted(UITextField textField)
{
if (MyParent.EditMask != "")
{
textField.Text = MyParent.EditMask.Replace("#", "_");
// Set cursor position
int position = textField.Text.IndexOf("_");
NSRange therange = new NSRange(position, 0);
UITextPosition start = textField.GetPosition(textField.BeginningOfDocument, therange.Location);
UITextPosition end = textField.GetPosition(start, therange.Length);
textField.SelectedTextRange = textField.GetTextRange(start, end);
}
}
public override bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
{
int fieldlength = 10; // MyParent.EditMask.Length;
string text = textField.Text;
int NeedASpace = MyParent.EditMask.IndexOf(" ");
string newText = "";
if (text == "")
{
newText = MyParent.EditMask.Replace("#", "_");
} else
{
newText = text;
}
string totalChar = newText.Replace(" ", "");
totalChar = totalChar.Replace("(", "");
totalChar = totalChar.Replace(")", "");
totalChar = totalChar.Replace("-", "");
totalChar = totalChar.Replace("_", "");
int val;
if ((totalChar + replacementString).Length <= fieldlength) {
if (replacementString != "")
{
index = newText.IndexOf("_");
StringBuilder sb = new StringBuilder(newText);
char character = char.Parse(replacementString);
sb[index] = character;
newText = sb.ToString();
textField.Text = newText;
// Set cursor to next position, this works flawlessly
NSRange therange = new NSRange(index, 0);
UITextPosition start = textField.GetPosition(textField.BeginningOfDocument, therange.Location + 1);
UITextPosition end = textField.GetPosition(start, therange.Length);
textField.SelectedTextRange = textField.GetTextRange(start, end);
newText = "";
}
else
{ // Still working the backspace key, so not done here yet.
if (text.Length > 1)
{
newText = text.Substring(0, text.Length - 1);
}
else
{
newText = "";
}
}
}
return Int32.TryParse(newText, out val);
}
}