Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Unable to select/copy/paste text in Xamarin.Forms Editor or custom Borderless Editor using renderer

Nidhi_SoodNidhi_Sood Member ✭✭
edited April 2020 in Xamarin.Forms

Hello @JamesMontemagno ,

I've implemented Borderless Editor in my Xamarin.Forms application using Custom EditorRenderer. But I'm facing an issue that the editor text is not selectable at all in both cases, neither in Forms editor control nor in Rendered native editor control. My app has the functionality to let user copy paste the text in the editor while typing, like in any other text editing app. This is a basic feature in most of the apps and is by default there. But it's not working in my app. I've tried enabling it through

Control.SetTextIsSelectable(true);

but still it's not working. I've tried other things too, like:

Control.CustomSelectionActionModeCallback = new CustomSelectionActionModeCallback(); Control.CustomInsertionActionModeCallback = new CustomInsertionActionModeCallback();

But nothing is working at all and text is not getting selected even a single word. Does anyone has any idea about this issue? How can I make the text selectable and allow default copy paste feature in custom editor?

Thanks in advance !

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    I've implemented Borderless Editor in my Xamarin.Forms application using Custom EditorRenderer.

    Could you post the code ?

    I test on normal Editor , it works fine .

  • Nidhi_SoodNidhi_Sood Member ✭✭

    Here's my code in Xaml:

    <renderer:BorderlessEditor Grid.Row="1" x:Name="UserTextEditorAndroid" BackgroundColor="{StaticResource WhiteColor}" HeightRequest="350" Margin="20,2" MaxLength="1024" IsReadOnly="{Binding Source={x:Reference LongTextTemplate}, Path=Editable, Converter={StaticResource InverseBool}}" />

    And the custom render code is:

    `protected override void OnElementChanged(ElementChangedEventArgs e)
    {
    base.OnElementChanged(e);
    if (e.OldElement == null)
    {
    Control.Background = null;

                var layoutParams = new MarginLayoutParams(Control.LayoutParameters);
                layoutParams.SetMargins(0, 0, 0, 0);
                LayoutParameters = layoutParams;
                Control.LayoutParameters = layoutParams;
                Control.SetPadding(0, 0, 0, 0);
                SetPadding(0, 0, 0, 0);
    
                Control.VerticalScrollBarEnabled = false;
            }
        }`
    
  • ColeXColeX Member, Xamarin Team Xamurai

    The issue is caused by the property IsReadOnly , it indicates whether user should be prevented from modifying(select/copy/paste) the text. Default is false.

    When the value is true , you can't edit the text .

  • JohnHardmanJohnHardman GBUniversity admin

    @Nidhi_Sood said:
    the editor text is not selectable at all in both cases

    The IsReadOnly property results in different behavior of different platforms. On UWP, even with IsReadOnly = true the user can still Select and Copy from the Editor (but not Cut or Paste). However, on Android, is seems that even Select and Copy are inoperable. I haven't done it for Editor, but for Entry ended doing some pretty hacky stuff to get something akin to a readonly Entry that still allowed Select and Copy. I expect the similar hacky stuff would be required for the Editor. TBH, I'm surprised that the Xamarin.Forms Entry and Editor do not do this out-of-the-box.

    cc @ColeX

  • System.AshSystem.Ash AUMember ✭✭

    @JohnHardman I'm often surprised how many things XF doesn't support out of the box, forcing you to go implement custom renderers and hack stuff - even for such basic things like text copy.
    Here's how you do it in React-Native:
    <Text selectable={true}>Text that can be selected with native actions on iOS/Android</Text>

    Done. Like...c'mon XF team, seriously...
    I'm by no way a proponent of RN, in fact I despise it, but geez it makes UI easy.

  • elashielashi Member ✭✭
    edited March 27

    Please vote up this enhancement: "Enable selecting and copying text in read only Editor and/or Entry"
    in Xamarin : https://github.com/xamarin/Xamarin.Forms/issues/14071
    and in Maui : https://github.com/dotnet/maui/issues/605

  • JohnHardmanJohnHardman GBUniversity admin

    @elashi said:
    Please vote up this enhancement: "Enable selecting and copying text in read only Editor and/or Entry"
    in Xamarin : https://github.com/xamarin/Xamarin.Forms/issues/14071
    and in Maui : https://github.com/dotnet/maui/issues/605

    There are at least two open issues already logged that relate to this
    https://github.com/xamarin/Xamarin.Forms/issues/12930 (which I logged when trying to overcome XF's accessibility limitations highlighted the problem again)
    and
    https://github.com/xamarin/Xamarin.Forms/issues/5843

    The latter has been open for nearly two years and still no sign of it being fixed. This really should be done in the XF/MAUI core, but given how long this has been outstanding, I'd suggest working around it in your own code using custom controls/renderers etc (and submitting any solution to Xamarin/Microsoft if appropriate)

Sign In or Register to comment.