c# Xamarin UITextField 设置光标位置 [英] c# Xamarin UITextField setting the cursor position

查看:43
本文介绍了c# Xamarin UITextField 设置光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 UITextField 的光标定位在与另一个先前文本字段完全相同的位置.我将光标在第一个文本字段中的位置设为 nint index = txtToField.GetOffsetFromPosition(txtToField.BeginningOfDocument, txtToField.SelectedTextRange.Start);,它正确地获取了从开头开始的字符数我的光标当前所在的文本字段.经过一些研究,并使用以下问题:

I need to position the cursor of a UITextField in the exact same position as another previous text field. I get the position of the cursor in the first text field as nint index = txtToField.GetOffsetFromPosition(txtToField.BeginningOfDocument, txtToField.SelectedTextRange.Start);, which correctly gets the number of characters from the beginning of the textfield my cursor is currently in. After some research, and using the question below:

设置 UITextField 的光标位置

我尝试使用我之前获得的索引来实现问题的解决方案,以在文本字段中设置光标位置.这不起作用,在将文本字段设为第一响应者并运行后:

I have tried to implement the question's solution to setting the cursor position in a textfield, using the index I previously got. This doesn't work, and after making the text field the first responder and running:

txtTo.BecomeFirstResponder();
UITextPosition positionSet = txtTo.GetPosition(txtTo.BeginningOfDocument, position);
txtTo.SelectedTextRange = txtTo.GetTextRange(positionSet, positionSet);

它会自动将光标置于 UITextField 的末尾.此外,我尝试检查 SelectedTextRange 方法是否按预期工作,但是在尝试将文本字段中的所有文本设置为选中时:

It automatically puts your cursor at the end of the UITextField. Further, I attempted to check if the SelectedTextRange method worked as expected, however when trying to set all of the text in the text field as selected:

txtTo.BecomeFirstResponder();
txtTo.SelectedTextRange = txtTo.GetTextRange(txtTo.BeginningOfDocument, txtTo.EndOfDocument);

它还自动将光标置于 UITextField 的末尾,这是 BecomeFirstResponder() 的标准行为.SelectedTextRange 在当前版本的 Xamarin 中不起作用吗?

It also automatically puts the cursor to the end of the UITextField, which is a standard behaviour for BecomeFirstResponder(). Does SelectedTextRange not work in this current version of Xamarin?

我使用的是 Xamarin 7.6.10 版(内部版本 27)和 Xamarin.iOS 12.0.0.15 版.

I am using version 7.6.10 (build 27) of Xamarin and Xamarin.iOS version 12.0.0.15.

推荐答案

原因:光标在 dafault 中文本的末尾.当您在方法 ViewDidLoad 中初始化一个 UITextField 并将其设置为 FirstResponder 时.视图仍未完成初始化.

Cause: The cursor is at the end of the text in dafault.When you init a UITextField and set it as FirstResponder in the method ViewDidLoad.The view is still not finish init.

解决方案:您可以在方法 EditingStarted 中调用这些方法.不要忘记设置委托.

Solution: You can call these method in the method EditingStarted .And don't forget set the delegate.

public partial clas xxxViewController:UIViewController,IUITextFieldDelegate

txtTo.WeakDelegate=this;

[Export("textFieldDidBeginEditing:")]
public void EditingStarted(UITextField textField)
{
  NSRange range = new NSRange(index, 0);
  UITextPosition start = textField.GetPosition(textField.BeginningOfDocument, range.Location);
  UITextPosition end = textField.GetPosition(start, range.Length);
  textField.SelectedTextRange = textField.GetTextRange(start, end);
} 

如果您确实想在 ViewDidLoad 中调用它们.您可以设置延迟(例如 0.1s).

If you do want to call them in ViewDidLoad .You can set a delay (for example 0.1s).

//...
this.PerformSelector(new Selector("MoveCursorPosition:"),txtTo,0.1);
//...

[Export("MoveCursorPosition:")]
public void MoveCursorPosition(UITextField textField)
{
  NSRange range = new NSRange(index, 0);
  UITextPosition start = textField.GetPosition(textField.BeginningOfDocument, range.Location);
  UITextPosition end = textField.GetPosition(start, range.Length);
  textField.SelectedTextRange = textField.GetTextRange(start, end);
}

这篇关于c# Xamarin UITextField 设置光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆