Home > Enterprise >  Font Awesome code as string not rendering as expected
Font Awesome code as string not rendering as expected

Time:05-11

I have a Label like this:

              new Label
                {
                    FontFamily = FontAwesome.Light,
                    FontSize = 50,
                    TextColor = Palette.Common.Accent,
                    BackgroundColor = Color.Transparent,
                    HorizontalTextAlignment = TextAlignment.Start,
                    VerticalTextAlignment = TextAlignment.Center,
                    HorizontalOptions = LayoutOptions.Start,
                    VerticalOptions = LayoutOptions.Center
                }
                .Bind(Label.TextProperty, nameof(MyFieldModel.iconCode))

iconCode in MyFieldModel is a string.

string iconCode = "f135";

This just renders as literal string f135 instead of a Rocket. (f135 is code for a rocket in fontawesome).

But when I change it to:

string iconCode = "\uf135";

It renders as Rocket.

If I get icon code input as string like "f135". How do I convert it to \uf135 format in code.

I tried all the following and none are working:

// try 1
string iconCode = "\\u"   myCodeString;
// try 2
string iconCode = @"\u"   myCodeString;
// try 3
StringBuilder sb = new StringBuilder();
sb.AppendFormat("U {0:X4} ", iconCode);

How do I make it work ?

CodePudding user response:

Based on https://stackoverflow.com/a/9303629/199364, try

= System.Text.RegularExpressions.Regex.Unescape(@"\u"   myCodeString);

For example, if myCodeString contains "f135", this is equivalent to

= System.Text.RegularExpressions.Regex.Unescape(@"\uf135");

which should give you the same result as:

= "\uf135";

a string containing that single unicode character.

  • Related