Steven Marcus
Steven Marcus

Reputation: 391

Scroll to Top of Editor Control

I would like to scroll to the first character of the editor control. Currently when new data is imported into the control, the control scrolls to the bottom which isn't the behavior I want. I don't see a clear way to accomplish this.

This needs to be set at load only as the user needs the ability to type without the cursor jumping to the begging during typing.

enter image description here


The following works for me which I've modified from the answer below. This is set in the Custom Rendered for Android:

        protected override void OnVisibilityChanged(global::Android.Views.View changedView, [GeneratedEnum] ViewStates visibility)
    {
        base.OnVisibilityChanged(changedView, visibility);

        Control.SetSelection(0);
    }

Upvotes: 0

Views: 204

Answers (1)

Junior Jiang
Junior Jiang

Reputation: 12723

You can custom a CustomEditorRenderer in native solution to achieve that .

Android :

[assembly: ExportRenderer(typeof(Editor), typeof(CustomEditorRenderer))]
namespace AppEntryTest.Droid
{
    class CustomEditorRenderer : EditorRenderer
    {
        public CustomEditorRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            
            //set position from 0 
            Control.SetSelection(0);

        }
}

The effect :

enter image description here

iOS :

[assembly: ExportRenderer(typeof(Editor), typeof(CustomEditorRenderer))]
namespace AppEntryTest.iOS
{
    class CustomEditorRenderer : EditorRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
        {
            base.OnElementChanged(e);

            Control.BecomeFirstResponder();

            NSRange range;
            range.Location = 0;
            range.Length = 0;
            Control.SelectedRange = range;
        }

}

The effect :

enter image description here

=============================Update=============================

First , create renderer class in each platform :

enter image description here

Uesd in Xaml as follow :

<Editor Text="{Binding EditorValue}" />

In addition , also can create a custom Editor in Forms :

public class MyEditor : Editor 
{

}

Used in Xaml : <local:MyEditor Text="{Binding EditorValue}" />

In rederer class , assembly also need to modify to use the custom Editor .

[assembly: ExportRenderer(typeof(MyEditor), typeof(CustomEditorRenderer))]

Upvotes: 1

Related Questions