Add Strikethrough formatting to your Xamarin label and other UI components

Posted: May 13, 2018 in Development, Software Development, Xamarin
Tags: , , , , ,

I had the need today to display strikethrough text in a Xamarin Forms app. The built-in label control didn’t support such formatting. So, leaning on Unicode’s strikethrough character set, I wrote a function to convert any string to a strikethrough string. To be fair, this works great for the normal character set, so I feel it’s good for most things. Please let me know if your mileage varies.

Business case: I needed to show a “Was some dollar amount” value. Like “Was $BLAH, and Now BLAH!”

In my class, I simply called into my strikethrough converter, as follows:

The property:

public string StrikeThroughValueText => StrikeThroughValue.HasValue ? $"{ConvertToStrikethrough(StrikeThroughValue.Value.ToString("C"))}" : "???";

The function:

private string ConvertToStrikethrough(string stringToChange)
{
    var newString = "";
    foreach (var character in stringToChange)
    {
        newString += $"{character}\u0336";
    }
 
    return newString;
}

Enjoy! I hope this helps you 🙂

Link: More about why this works: Combining Long Stroke Overlay.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s