Copy text from label in xamarin.forms

im completely new to xamarin.forms. I started building an application that would display a large amount of text extracted from a book. At the moment I am adding text to the label, however the user cannot copy the text he sees on the screen to any of the three platforms? Is there a way to enable copying of text from a label in xamarin.forms? I also tried with the editor, it works to copy the data when clicked in the editor, however the user can enter characters that I don't want them to do. I just want to show the text that I have read in the stream and give the user the option to copy the amount of text they want. Any help would be appreciated!

+3


source to share


2 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";
        }

      

+2


source


My advice would be to use webview instead if you want to display text that the user should be able to copy using their own copy mechanisms. The text should be in the html code that is added to the webview. Take a look at the following guide to do this:



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

0


source







All Articles