Forum Xamarin Xamarin.Forms

hyperlink in label

agassishajuagassishaju Member ✭✭
edited April 8 in Xamarin.Forms

Hi, i wanted to make a hyperlink in label i have done the xaml part coding

can somebody help me with the C# coding. Thanks

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Here is the code:

    var formattedString = new FormattedString();
    
    formattedString.Spans.Add(new Span { Text = "LinkedIn: ", TextColor = Color.Black, FontSize = 18 });
    
    var span = new Span { Text = "https://wwww.linkedin.com", TextColor = Color.Blue, FontSize = 11 };
    TapGestureRecognizer tapGesture = new TapGestureRecognizer();
    tapGesture.SetBinding(TapGestureRecognizer.CommandProperty, "TapCommand");
    span.GestureRecognizers.Add(tapGesture);
    span.TextDecorations = TextDecorations.Underline;
    formattedString.Spans.Add(span);
    
    var label = new Label { FormattedText = formattedString };
    

    refer to this documentation for more details about Formatted text:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/label#formatted-text

  • agassishajuagassishaju Member ✭✭

    is this the .cs code? because when i pasted it and made some changes it shows no difference, like when i touch the link nothing is happening. i really thank you for your effort but its not working.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Have you assigned the page's binding context?
    I created a sample here.
    This is the effect:

  • agassishajuagassishaju Member ✭✭

    I'm new into this game i actually don't know how to play with it. this is what i did with your code

  • LandLuLandLu Member, Xamarin Team Xamurai

    This code is used to create a label control. But if you want to display it on your page we need to add it to the view hierarchy like what I have done in my sample:

    layout.Children.Add(label);
    

    layout is my root layout's name which has been assigned in xaml:

    <StackLayout x:Name="layout">
    </StackLayout>
    

    I used binding here to fire a command in the view model. Follow this documentation to know more about binding:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/

  • agassishajuagassishaju Member ✭✭

    what i actually wanted was to display my linkedin website on my app when i touch it. can you help me out in coding that.

  • LandLuLandLu Member, Xamarin Team Xamurai

    If you want to open a specific URL using browser we could try Launcher:
    https://docs.microsoft.com/en-us/xamarin/essentials/launcher?context=xamarin/xamarin-forms&amp;tabs=android
    Call this method in your command like:

    await Launcher.OpenAsync("lyft://ridetype?id=lyft_line");
    
  • agassishajuagassishaju Member ✭✭

    i tried this but still i cant find out whats going wrong here, it doesn't open up the link when i touch the label. this is my .cs code

  • LandLuLandLu Member, Xamarin Team Xamurai

    Will this TapCommand be triggered when you click the text?
    How did you bind your tapgesture to this command?
    Have you tried my sample above?
    You could put the await Launcher.OpenAsync("") method in the command to see the effect.

  • agassishajuagassishaju Member ✭✭

    I'm sorry i didnt get you . uh can you please type it in or just edit the picture to make me understand. im really sorry im new in to this.

  • agassishajuagassishaju Member ✭✭

    yes its working, thank you so much bro. i also have one more question, is there any way that i can set the position of this span or text because on my contact page the linkedin link is at the bottom below the button, actually i wanted it above the button, below the gender label. can you please help me out on that too.

Sign In or Register to comment.