如何在具有多行自动换行的 UITextView 中找到光标 Y 位置?
How to find the cursor Y position in an UITextView having multiple lines as word wraps?
我需要在多行的 UiTextView 中找到光标位置或焦点位置。
您可以使用以下代码获取光标的当前position
和当前rect
:
public partial class ViewController : UIViewController
{
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UITextView textF = new UITextView();
textF.Frame = new CoreGraphics.CGRect(30,20,200,50);
textF.Text = "testtesttesttesttesttesttesttesttesttest";
textF.Delegate = new myTextDelegate();
View.Add(textF);
}
}
public class myTextDelegate : UITextViewDelegate {
public override bool ShouldChangeText(UITextView textView, NSRange range, string text)
{
//To get the current Position
var startPoint = textView.BeginningOfDocument;
var selectRange = textView.SelectedTextRange;
var currentPoint = textView.GetOffsetFromPosition(startPoint, selectRange.Start);
Console.WriteLine(currentPoint);
//To get the current Rect
CoreGraphics.CGRect caretRect = textView.GetCaretRectForPosition(selectRange.End);
Console.WriteLine(caretRect);
return true;
}
}
参考: and cursor-position-in-relation-to-self-view
您可以通过不同的方式在 UITextView 中获取光标的 CGPoint(X 和 Y 位置)。但是你需要找到光标相对于 self.view(或 phone 屏幕边框)的位置吗?如果是这样,我为您翻译了 this 对 C# 的回答:
var textView = new UITextView();
UITextRange selectedRange = textView.SelectedTextRange;
if (selectedRange != null)
{
// `caretRect` is in the `textView` coordinate space.
CoreGraphics.CGRect caretRect = textView.GetCaretRectForPosition(selectedRange.End);
// Convert `caretRect` in the main window coordinate space.
// Passing `nil` for the view converts to window base coordinates.
// Passing any `UIView` object converts to that view coordinate space.
CoreGraphics.CGRect windowRect = textView.ConvertRectFromCoordinateSpace(caretRect, null);
}
else
{
// No selection and no caret in UITextView.
}
我需要在多行的 UiTextView 中找到光标位置或焦点位置。
您可以使用以下代码获取光标的当前position
和当前rect
:
public partial class ViewController : UIViewController
{
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UITextView textF = new UITextView();
textF.Frame = new CoreGraphics.CGRect(30,20,200,50);
textF.Text = "testtesttesttesttesttesttesttesttesttest";
textF.Delegate = new myTextDelegate();
View.Add(textF);
}
}
public class myTextDelegate : UITextViewDelegate {
public override bool ShouldChangeText(UITextView textView, NSRange range, string text)
{
//To get the current Position
var startPoint = textView.BeginningOfDocument;
var selectRange = textView.SelectedTextRange;
var currentPoint = textView.GetOffsetFromPosition(startPoint, selectRange.Start);
Console.WriteLine(currentPoint);
//To get the current Rect
CoreGraphics.CGRect caretRect = textView.GetCaretRectForPosition(selectRange.End);
Console.WriteLine(caretRect);
return true;
}
}
参考:
您可以通过不同的方式在 UITextView 中获取光标的 CGPoint(X 和 Y 位置)。但是你需要找到光标相对于 self.view(或 phone 屏幕边框)的位置吗?如果是这样,我为您翻译了 this 对 C# 的回答:
var textView = new UITextView();
UITextRange selectedRange = textView.SelectedTextRange;
if (selectedRange != null)
{
// `caretRect` is in the `textView` coordinate space.
CoreGraphics.CGRect caretRect = textView.GetCaretRectForPosition(selectedRange.End);
// Convert `caretRect` in the main window coordinate space.
// Passing `nil` for the view converts to window base coordinates.
// Passing any `UIView` object converts to that view coordinate space.
CoreGraphics.CGRect windowRect = textView.ConvertRectFromCoordinateSpace(caretRect, null);
}
else
{
// No selection and no caret in UITextView.
}