Select text in label and copy it

MikilllMikilll Member ✭✭✭

In Xamarin Forms when I am creating a label I cannot select it and copy. How can I make a label in Xamarin Forms copyable? Currently, for me its strange behavior that user is not able to select and copy text in my application.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The fact that you're even trying to do this indicates you're not building along good MVVM designs. That is going to get you painted into a corner really quickly in 100 other ways.

    Your label should be binded to a property on your ViewModel. So if you need that text you use the property not you UI. Your UI should NEVER be used like it is your private variable storage.

    your code should run run even if there were no UI. If your code is depended on the UI then you should stop and fix the architecture.

  • MikilllMikilll Member ✭✭✭
    @ClintStLaurent Wtf? I know MVVM. Is your reply related to my problem?
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Yes. You said you're trying to copy the text off a Label. Why are you trying to work off the UI Label if you're using MVVM? You shouldn't be trying to take the text off the Label; you should be using the string property the Label is binded to.

  • seanydaseanyda GBMember ✭✭✭✭✭

    It's just not a feature of the UI to be able to copy and paste text from a Label. Have you tried making a custom renderer to make a "borderless" disabled Entry as a workaround? It'll look and behave like a Label but you can copy and paste it too.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    How about just adding a TapGestureRecognizer to the Label. Tadaa... You can tap it.
    In the handler for the tap, you copy the property the Label is binded to. @JohnHardman already gave you a link for a copy/paste/clipboard nuget.

    What am I missing about this being such a big problem? Seems like making a mountain out of a mole hill.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think problem is to select text in a Label. I don't know if it is possible.
    BTW I think in next XF release should exists an Entry with a ReadOnly property

    https://github.com/xamarin/Xamarin.Forms/issues/1678

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Again... Shouldn't be trying to get the Label.Text in the first place.
    Otherwise... The Sender for the event IS the Label... so cast the sender to label, then get the .Text off it - if you want to continue to do it like 1998 WinForms project.

  • seanydaseanyda GBMember ✭✭✭✭✭

    @ClintStLaurent said:
    Again... Shouldn't be trying to get the Label.Text in the first place.
    Otherwise... The Sender for the event IS the Label... so cast the sender to label, then get the .Text off it - if you want to continue to do it like 1998 WinForms project.

    I think what @Mikilll is looking for is a Label where the user can highlight it like the behaviour of text in a WebView. This would be irrelevant to the MVVM pattern because the ViewModel doesn't need to know whether or not the user has highlighted the text to copy and paste.

  • JohnHardmanJohnHardman GBUniversity mod

    @AlessandroCaliaro said:
    I think problem is to select text in a Label. I don't know if it is possible.
    BTW I think in next XF release should exists an Entry with a ReadOnly property

    https://github.com/xamarin/Xamarin.Forms/issues/1678

    It'll be interesting to see if it works fully on first release. Getting the behavior as expected across platforms, including supporting Copy and Select All, but not Cut or Paste, is non-trivial. It doesn't take a huge amount of code, but it involves going deeper into platform-specifics than most XF people normally do.

  • JohnHardmanJohnHardman GBUniversity mod
    edited July 2018

    @ClintStLaurent said:
    How about just adding a TapGestureRecognizer to the Label. Tadaa... You can tap it.
    In the handler for the tap, you copy the property the Label is binded to. @JohnHardman already gave you a link for a copy/paste/clipboard nuget.

    What am I missing about this being such a big problem? Seems like making a mountain out of a mole hill.

    We don't actually know what the OP's detailed requirements are. Does @Mikilll want to copy the entire text, or be able to copy just parts of it? Does accessibility need to be supported? Does use of a physical keyboard need to be supported, including tab navigation? Do normal Windows key combinations such as Ctrl-A and Ctrl-C need to be supported? Depending on the answers to those questions, it's possible that a gesture recognizer could suffice. However, I know when I implemented this, the answers to those questions meant going with a customised Entry that gave read-only behavior, whilst supporting Copy, Select All, Ctrl-C, Ctrl-A, tabbing etc.

    Using a customised Entry also means that the View Model is not used. However, if a gesture recognizer were used with a Label then it would make sense to use the View Model.

    @Mikilll - @seandya is correct when saying "It's just not a feature of the UI to be able to copy and paste text from a Label". If trying to implement anything more than tap on the Label and its entire text gets copied to the clipboard, then Label is inherently the wrong view to be using.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @JohnHardman You're completely right that we don't know what @Mikilll fully wants beyond what the question says. Just like all of his other posts its almost meaningless and contains very little useful information beyond what could be typed in 10 seconds.

    So I answer the question as posted instead of the question, and the deeper 10 questions not asked. After all, he's already been told 5 different ways that what he's asking to do to is not normal, not expected, not mobile, not MVVM - but he still wants to do it anyway. So... there's one way to do this horrible thing the wrong way.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @seanyda said:
    I think what @Mikilll is looking for is

    NO offense to you... But we shouldn't have to guess what he is looking for. He came to us looking for help. Why should we have to guess and interrogate him to get any explanations?

    I'm kinda done trying to guess what people mean or want or trying to read between the lines of a question they barely take 10 seconds to write. If @Mikill can't be bothered with actually writing what he wants in detail, then why should the rest of us get twisted up in trying to figure it out? He devotes 60 seconds to his own issue while everyone else invests 10x as much time. Doesn't make any sense. Worst of all it takes time away from those people that do take the time to post good, meaningful, detailed questions that show they've made an effort before even posting.

    If I could unsubscribe to this thread I would - but that feature doesn't exist on this forum.

    But from now own, I'm done even answer questions like this where the poster can't be bothered to make any kind of effort on the question - and where I do answer and the poster totally ignores all the advice given, I'm done trying to convince them. Just like this guy, they're bound and determined to do it they way they are going to do it despite what anyone else with a couple decades of experience says.

  • 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.

  • chiwing4chiwing4 Member

    To anyone finding a simpler solution:
    Try using a WebView. Text in WebView is copy-able and not editable.

    var html = new HtmlWebViewSource {
      Html = "your text here"
    };
    
    webView.Source = html;
    
Sign In or Register to comment.