Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Selecting Label Text for Copy and Paste

KrystinKrystin USMember, University

I am using labels to display contact info for a CRM application. I can't seem to select label text on any of the platforms, blocking me from copying and pasting contact information. Is there a property that I am not finding to allow copy and paste? Or do I need to use something other than a label?

Posts

  • Have you ever figured this one out?

  • I need to do something similar, did you find a solution?

  • JulianPasqueJulianPasque USMember ✭✭✭

    I would like to do the same. Did anyone of you guys find a solution for?

  • JohnHardmanJohnHardman GBUniversity admin
    edited August 2016

    @JulianPasque -

    Label does not natively support Copy. Entry and Editor do. So it's either a case of changing to Entry or Editor (and possibly making them read only), or writing a custom renderer for Label.

    Alternatively, if appropriate, add a Copy option to the toolbar and have that option create a text equivalent of what is on screen and put this in the clipboard.

  • XavierPerseguersXavierPerseguers CHMember ✭✭✭
    edited September 2016

    I'd be interesting into seeing a solution here, I'm extensively using TableView to show data and already have a few custom cells and renderers but I really miss the standard "select part of the contents and copy to clipboard" feature. Ideally, this should be some attribute of Label and the various *Cell controls and behave natively:

    image

    image

  • knottydevknottydev USMember ✭✭
    edited December 2017

    Not being able to select text should be the feature option, not the default. Has anyone asked for this in the past? Is there a reason it's missing?

    Perhaps it would make it a heavier control, but the use case of locking text is very uncommon. And, because the opposite use case is almost 100% common, the Label control is simply useless to me.

    I can see this being overlooked by many developers and the overall quality of many Forms apps thus suffering. As a lead in my organization, I will not allow this kind of thing to fly as just an inconvenience to users. By default, everything in the Beta version should be selectable and QA will absolutely fail it if it's not a specific requirement not to be locked. As I said... Labels are useless. Xamarin could remove them entirely and I wouldn't complain.

  • BradChase.2654BradChase.2654 USMember ✭✭✭
    edited December 2017

    This might help get ya closer:

    `
    public class EntrySelectEffect : PlatformEffect
    {
    public void SelectText(int in_Index, int in_Length)
    {

    if WPF

            System.Windows.Controls.TextBox textBox = this.Control as System.Windows.Controls.TextBox;
            Entry entry = Element as Entry;
    
            if (textBox == null || entry == null)
                return;
    
            textBox.Select(in_Index, in_Length);
    

    endif

    if IOS

            UITextView textBox = this.Control as UITextView;
            Entry entry = Element as Entry;
            if (textBox == null || entry == null )
                return;
    
            textBox.SelectedTextRange = textBox.GetTextRange(textBox.GetPosition(textBox.BeginningOfDocument, in_Index), textBox.GetPosition(textBox.BeginningOfDocument, in_Index + in_Length));
    

    endif

    if DROID

            EditText textBox = Control as EditText;
            Entry entry = Element as Entry;
    
            if (textBox == null || entry == null )
                return;
    
            textBox.SetSelection(in_Index, in_Length);
    

    endif

    if UWP

            TextBox textBox = Control as TextBox;
            Entry entry = Element as Entry;
    
            if (textBox == null || entry == null)
                return;
    
            textBox.Select(in_Index, in_Length);
    

    endif

        }
    
        public void SelectText( string in_SelectText )
        {
    

    if WPF

            System.Windows.Controls.TextBox textBox = this.Control as System.Windows.Controls.TextBox;
            Entry entry = Element as Entry;
    
            if (textBox == null || entry == null)
                return;
    
            int index = entry.Text.IndexOf(in_SelectText);
            int length = in_SelectText.Length;
    
            textBox.Select(index, length);
    

    endif

    if IOS

            UITextView textBox = this.Control as UITextView;
            Entry entry = Element as Entry;
            if (textBox == null || entry == null )
                return;
    
            int index = entry.Text.IndexOf(in_SelectText);
            int length = in_SelectText.Length;
    
            textBox.SelectedTextRange = textBox.GetTextRange(textBox.GetPosition(textBox.BeginningOfDocument, index), textBox.GetPosition(textBox.BeginningOfDocument, index + length));
    

    endif

    if DROID

            EditText textBox = Control as EditText;
            Entry entry = Element as Entry;
    
            if (textBox == null || entry == null )
                return;
    
            int index = entry.Text.IndexOf(in_SelectText);
            int length = in_SelectText.Length;
    
            textBox.SetSelection(index, length);
    

    endif

    if UWP

            TextBox textBox = Control as TextBox;
            Entry entry = Element as Entry;
    
            if (textBox == null || entry == null )
                return;
    
            int index = entry.Text.IndexOf(in_SelectText);
            int length = in_SelectText.Length;
    
            textBox.Select(index, length);
    

    endif

        }
    
        protected override void OnAttached()
        {
    
        }
    
        protected override void OnDetached()
        {
    
        }
    
        protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
        {
            base.OnElementPropertyChanged(args);
        }       
    }
    

    `

    Ive never been able to use the code function on this forum but if you can get the text out of it, its there.

  • JohnHardmanJohnHardman GBUniversity admin

    @BradChase.2654 - re. "Ive never been able to use the code function on this forum but if you can get the text out of it, its there."

    Where your post has a single back-tick before and after the code block, replace the single back-ticks by three back-ticks with no space between the back-ticks.

  • anna.domashychanna.domashych USMember ✭✭
    edited August 2018

    To let user select and copy a part of text but not allow to modify it I've used an Editor control from Xamarin.Forms, which I customized with renderers on each platform.

    Check out my posts with code samples for Xamarin.Forms
    medium.com/@anna.domashych/selectable-read-only-multiline-text-field-in-xamarin-forms-69d09276d580

    You can find more explanation in posts about native Android
    medium.com/@anna.domashych/selectable-read-only-multiline-text-field-on-android-169c27c55408
    and native iOS
    medium.com/@anna.domashych/selectable-read-only-multiline-text-field-on-ios-7a5f5a44bd9

  • anna.domashychanna.domashych USMember ✭✭
    edited August 2018

    To let a user select and copy a part of the text but not allow to modify it I've used an Editor control from Xamarin.Forms, which I customized with renderers on each platform.

    Check out my post with code samples for Xamarin.Forms .

    You can find more explanation in posts about native Android and native iOS.

Sign In or Register to comment.