Something like This ?
Make a Class with the name LinksLabel
public class LinksLabel : Label
{
public static BindableProperty LinksTextProperty = BindableProperty.Create(nameof(LinksText), typeof(string), typeof(LinksLabel), propertyChanged: OnLinksTextPropertyChanged);
[Obsolete]
private readonly ICommand _linkTapGesture = new Command<string>((url) => Device.OpenUri(new Uri(url)));
public string LinksText
{
get => GetValue(LinksTextProperty) as string;
set => SetValue(LinksTextProperty, value);
}
private void SetFormattedText()
{
var formattedString = new FormattedString();
if (!string.IsNullOrEmpty(LinksText))
{
var splitText = LinksText.Split(' ');
foreach (string textPart in splitText)
{
var span = new Span { Text = $"{textPart} " };
if (IsUrl(textPart)) // a link
{
span.TextColor = Color.DeepSkyBlue;
span.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = _linkTapGesture,
CommandParameter = textPart
});
}
formattedString.Spans.Add(span);
}
}
this.FormattedText = formattedString;
}
private bool IsUrl(string input)
{
return Uri.TryCreate(input, UriKind.Absolute, out var uriResult) &&
(uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
}
private static void OnLinksTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var linksLabel = bindable as LinksLabel;
linksLabel.SetFormattedText();
}
}
Add to the Class
using System.Windows.Input;
using Xamarin.Forms;
Then in Xaml
<StackLayout>
<custom:LinksLabel
HorizontalTextAlignment="Center"
LinksText="This Xamarin.Forms Forums question can be visited at https://forums.xamarin.com/discussion/161082/make-multiple-links-in-labels-clickable#latest but Google only at https://www.google.com/"
TextColor="DarkGray" />
</StackLayout>