AAHN
AAHN

Reputation: 401

Detect Phone Number & Link in xamarin.Form

assume we have the following text :

Contact us on 015546889 or [email protected]

How I can display the above text in same label in xamarin.forms and handle click on email by send email and handle phone call by click on the number.

I can use the the following code to make clickable label

Label label = new Label;
     label.GestureRecognizers.Add(new TapGestureRecognizer()
    {
         Command = new Command(() => {
           //do some function here
        })
   });

How to hyperlink same as messaging app or Whatsapp application

Upvotes: 1

Views: 3123

Answers (3)

Fiwita
Fiwita

Reputation: 3

Same response as AAHN, https://theconfuzedsourcecode.wordpress.com/tag/xamarin-hyperlink-label/ But i dont have enoguh reputation to comment his answer.

Just an improvement in the android part, instead of overriding OnElementChanged, override CreateNativeControl so you add the mask there, that makes easier to get the styles of the xamarin.forms part correct because you are not overriding them.

    public class HyperLinkLabelRenderer : LabelRenderer
{
    private Context _context;
    public HyperLinkLabelRenderer(Context context) : base(context)
    {
        _context = context;
    }

    protected override TextView CreateNativeControl()
    {
        var view = base.CreateNativeControl();
        view.AutoLinkMask = MatchOptions.All;
        return view;
    }
}

Upvotes: 0

AAHN
AAHN

Reputation: 401

After a lot of search i found the perfect solution Here :
https://theconfuzedsourcecode.wordpress.com/tag/xamarin-hyperlink-label/

Hope this will help others :)

Upvotes: 4

baskren
baskren

Reputation: 1232

Check out the Label element in Forms9Patch. It has a HtmlText property that allows simple markup.

using System;
using Xamarin.Forms;
namespace Forms9PatchDemo
{
    public class LabelLink : ContentPage
    {
        public LabelLink()
        {
            var label = new Forms9Patch.Label
            {
                HtmlText = "Contact us on <a id=\"phone\" href=\"tel:+353015546889\">015546889</a> or <a id=\"email\" href=\"mailto:[email protected]\">[email protected]</a>"
            };
            label.ActionTagTapped += (object sender, Forms9Patch.ActionTagEventArgs e) =>
            {
                var id = e.Id;
                var href = e.Href;
                var uri = new Uri(e.Href);
                Device.OpenUri(uri);
            };
            Content = new StackLayout
            {
                VerticalOptions = LayoutOptions.Center,
                Children = {
                    new Label { Text = "Forms9Patch.Label.HtmlText <a> example" },
                    new BoxView { BackgroundColor = Color.Black, HeightRequest = 1 },
                    label
                }
            };
        }
    }
}

Note that the above example won't work on iOS emulators because the tel: and mailto: schemes are not supported. It does work on actual iOS devices.

Upvotes: 1

Related Questions