Copy text from label in xamarin.forms

2017-07-05 text xamarin.forms copy label xamarin.forms.entry

I'm quite new to Xamarin.Forms. I have started to create an application which will display large amount of text retrieved from a book. At the moment I add the text into a Label, however the user can't copy the text he sees on the screen in any of the three platforms.

Is there a way to enable copying of text from a label in Xamarin.Forms? I have tried with Editor as well, it works to copy the data when pushed into Editor, however user can enter characters which is what I don't want them to do. I just want to show the text that I have read in from the stream and make it possible for the user to copy the amount of text he wishes. Any help will be appreciated!

Answers

Can you try below

Page.Xaml

<Entry TextChanged="Handle_TextChanged" Text="EnteyText" Unfocused="Handle_Unfocused" Focused="Handle_Focused" x:Name="entry" HeightRequest="50" WidthRequest="100" BackgroundColor="Aqua"/>
<Button Text="Bind Text From Book" Clicked="Handle_Clicked" HeightRequest="50" WidthRequest="100" />

Page.Xaml.CS

    bool isFocused;
    string entryText="EnteyText";

    void Handle_Focused(object sender, Xamarin.Forms.FocusEventArgs e)
    {
        isFocused = e.IsFocused;
    }

        void Handle_Unfocused(object sender, Xamarin.Forms.FocusEventArgs e)
        {
            isFocused = e.IsFocused;
        }

        void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
        {
            if (!isFocused)
            {
                entryText = (sender as Entry).Text;
            }
            if (isFocused && entryText != e.NewTextValue)
            {
                (sender as Entry).Text = e.OldTextValue;
            }
        }

        void Handle_Clicked(object sender, System.EventArgs e)
        {
            entry.Unfocus();
            entry.Text = "Text from book";
        }

My advise would be to use webview instead if you want to display text that a user should be able to copy using the native copy mechanisms. The text will have to be in html code that is added to the webview. Look at the following guide for this:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=vswin

Related