I am looking for a way to make parts of the text of a Label in Xamarin Forms clickable, performing different functions, and ideally have different formatting. I understand that a clickable Label is straightforward and can be done something like this:
Label label = new Label { Text = "Click me" };
label.Click += () => { label.Text = "Clicked"; };
Essentially, I'm looking to mimic behavior in an app that I've done in native Android. My app must work for Android and iOS and ideally UWP. In Android, CharSequence objects can be added to a TextView (the control similar to Label). In Android, I might create a CharSequence object like this:
Runnable doSomething; //equivalent to C# Action; assuming initialized somewhere
String myString = "My Clickable String"; //the clickable string
Spannable span = Spannable.Factory.getInstance().newSpannable(myString); //this is the clickable text to add to TextView
span.setSpan(new ClickableSpan() {
@Override
public void onClick(View v) {
doSomething.run(); //perform the action
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(true); //make underlined
ds.setColor(Color.rgb(color.r, color.g, color.b)); //change the color
ds.setFakeBoldText(true); //make it bold
ds.setTextSize((float) largeFont); //change the text size
}
}, 0, a.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //style and function for the entire string
As you can see, it performs a function and has style attributes. I can then add this to TextView (Spannable implements Spanned which implements CharSequence). I can add multiple CharSequences in addition to non-clickable Strings as I desire.
I'm hoping for a way that I can do this in Xamarin but I haven't found a solution yet. I've considered the possibility of custom renderers though I feel that a cross-platform solution would be preferable, if possible. I'm far less familiar with iOS but it seems that it's possible to add NSMutableAttributedString objects to a UILabel to similar effect. I've also seen the FancyLabel object. I'm not quite sure how I would do this for UWP as I really didn't find much in my research.
Any assistance would be very appreciated!
TapGestureRecognizer. Would much rather have only the specific word clickable. - hvaughan3