Is there any way to copy the text of Label in Xamarin.Forms

jainankit2474jainankit2474 INMember ✭✭
edited May 16 in Xamarin.Forms

We are working on Xamarin cross-platform application, and we are using a custom Grid with the help of ListView and Label. And our customers want to copy the text of Label by using Ctrl + C or using Copy on Mouse Right click.

Is there any way to copy the text of Label.

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭

    Sure. There is a feature in this package Xamarin.Essentials which allows you to copy text to the clipboard.

    More specifically look here in the documentation:
    https://docs.microsoft.com/en-us/xamarin/essentials/clipboard?context=xamarin/xamarin-forms

  • JohnHardmanJohnHardman GBUniversity ✭✭✭✭✭

    @jainankit2474 - The out-of-the-box Label does not support the user interactions required to do this. You could create a custom renderer for Label, or you could create a read-only Entry with associated custom renderer (if it's just on UWP, the latter is easier).

    Alternatively, you could add a Button or ToolbarItem, that copies the text from the corresponding Label. It really depends on how you want the UI to look. That makes the UI a bit busier, but will work cross-platform using an appropriate plugin for doing the actual write to the clipboard.

  • jainankit2474jainankit2474 INMember ✭✭

    @JohnHardman: How can I create Custom Renderer for Label.?

  • JohnHardmanJohnHardman GBUniversity ✭✭✭✭✭

    @jainankit2474 said:
    @JohnHardman: How can I create Custom Renderer for Label.?

    Of the three options I mentioned in my previous post, that's the one I would not recommend.

    I'll add a fourth option for completeness. Replace the Label by an appropriately sized WebView that contains just the text that you currently show using the Label. The WebView will provide Copy functionality for you.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    But a bigger question is:

    Why are you trying to work directly off the Label like this is a 1998 WinForms project?

    The Label text should be coming from a binding to a property on your ViewModel. So instead of hacking the Label just copy/use the data the Label is binded to.

  • JoeMankeJoeManke USMember ✭✭✭✭

    The users of an app don't have access to the ViewModel data besides what is presented by the view, Clint.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I get that @JoeManke
    What I'm saying is that the the code executed from the copy command does have access to the ViewModel.
    That copy command shouldn't be targeting the Label.Text property: that's 1998 coding.
    The copy command should be using the string property that is backing the View. Logic targeting logic. Not ViewModel targeting UI causing a tightly coupled relationship that breaks good MVVM.

  • JohnHardmanJohnHardman GBUniversity ✭✭✭✭✭

    Of the four options listed above:

    • I simply would not do the custom Label renderer
    • If using the additional Button I (probably) would use the View Model, as per @ClintStLaurent
    • If using the custom Entry renderer, the Copy support would be provided by the native UI element
    • If using the WebView, the Copy support would be provided by the WebView
  • jainankit2474jainankit2474 INMember ✭✭

    @John Hardman: Why not to use custom Label Renderer.?

  • JohnHardmanJohnHardman GBUniversity ✭✭✭✭✭

    @jainankit2474 said:
    @John Hardman: Why not to use custom Label Renderer.?

    Too much effort required to skew the Label behavior to do what you want.

Sign In or Register to comment.