Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you allow users to copy and paste from an Xamarin.Forms label

How do you allow users to copy and paste from an Xamarin.Forms Label?

Click on the text on any platform the default settings don't allow highlighting and therefore copying and pasting.

Any help would be appreciated.

like image 688
Jhayes2118 Avatar asked Dec 19 '14 16:12

Jhayes2118


2 Answers

What you could do is wrap your label in a gesture recogniser:

<Label Text="Test">
    <Label.GestureRecognizers>
        <TapGestureRecognizer
            Tapped="YourFunctionToHandleMadTaps" 
            NumberOfTapsRequired="1" 
        />
   </Label.GestureRecognizers>
</Label>

This will trigger your function and in that function you can get to the clipboard and copy and paste. However I haven't been able to find an easy way to access the clipboard in Xamarin.Forms so you have to use the dependency service.

Xamarin.Forms Dependency service documentation

Here is how I did my clipboard data access. Please note that in my project I only needed to nab data from the clipboard so this code just shows you how to access the clipboard data:

  1. Create an interface in you X.F project eg:

        public interface IClipBoard
        {
            String GetTextFromClipBoard();
        }
    
  2. Implement the interface in your mobile projects:

    Android:
        public string GetTextFromClipBoard ()
        {
            var clipboardmanager = (ClipboardManager)Forms.Context.GetSystemService (Context.ClipboardService);
            var item = clipboardmanager.PrimaryClip.GetItemAt(0);
            var text = item.Text;
            return text;
        }
    
    iOs:
        public string GetTextFromClipBoard ()
        {
            var pb = UIPasteboard.General.GetValue ("public.utf8-plain-text");
            return pb.ToString ();
        }
    

Don't forget to add the Assembly bits at the top:

    iOs: [assembly: Dependency (typeof (ClipBoard_iOs))]
    Android: [assembly: Dependency (typeof (ClipBoard_Droid))]
  1. Call the dependency service from you X.F function

        public void YourFunctionToHandleMadTaps(Object sender, EventArgs ea)
        {
            var clipboardText = DependencyService.Get<IClipBoard> ().GetTextFromClipBoard ();
    
            YourFunctionToHandleMadTaps.Text = clipboardText;
        }
    
like image 130
I_Khanage Avatar answered Sep 21 '22 21:09

I_Khanage


Since I_Khanage provided only a half solution, I will post the full solution.

IClipboardService should be implemented for all the targeting platforms, in my case it is Android and iOS:

public interface IClipboardService
{
    string GetTextFromClipboard();
    void SendTextToClipboard(string text);
}

// iOS
public class ClipboardService : IClipboardService
{
    public string GetTextFromClipboard() => UIPasteboard.General.String;
    public void SendTextToClipboard(string text) => UIPasteboard.General.String = text;
}

// Android
public class ClipboardService : IClipboardService
{
    public string GetTextFromClipboard()
    {
        var clipboardmanager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);
        var item = clipboardmanager.PrimaryClip.GetItemAt(0);
        var text = item.Text;
        return text;
    }

    public void SendTextToClipboard(string text)
    {
        // Get the Clipboard Manager
        var clipboardManager = (ClipboardManager)Forms.Context.GetSystemService(Context.ClipboardService);

        // Create a new Clip
        var clip = ClipData.NewPlainText("YOUR_TITLE_HERE", text);

        // Copy the text
        clipboardManager.PrimaryClip = clip;
    }
}

The code is available on github.

Now just add a GestureRecognizer in order to trigger the tap event.

P.S.: Clipboard service is now available as a part of Xamarin.Essentials package, so there is no need to write one yourself.

like image 42
EvZ Avatar answered Sep 22 '22 21:09

EvZ