XAML TextBlock set special characters programmatically?
Asked Answered
P

2

9

I want to use font awesome (http://fortawesome.github.io/Font-Awesome/design.html) in XAML.

I have been able to easily get it to work via direct XAML, by creating a fonts folder and adding the font there, then in XAML:

<TextBlock FontFamily="Fonts/#FontAwesome">&#xf000;</TextBlock>

Displays a martini glass icon.

However, when adding it programmatically it just shows and invalid symbol like so: [], I tried the following:

XAML:

<TextBlock Name="textBlock"></TextBlock>

C#:

textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome");
textBlock.Text = HttpUtility.HtmlDecode("&#xf000;");

and the following which returns the literal string:

textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome");
textBlock.Text = "&#xf000;";

Any ideas?

Pantia answered 6/5, 2013 at 23:31 Comment(0)
M
7

try the following :

textBlock.FontFamily= new FontFamily(new Uri("pack://application:,,,/"), @"/Fonts/#FontAwesome"); // you should well reference your font else you will get a square
textBlock.Text = "\uf000";// \u (unicode escape char) instead of &#x

and if you want to preview your textblock XAML use

t_out.Text = XamlWriter.Save(textBlock);
Mudguard answered 7/5, 2013 at 0:22 Comment(1)
The URI seemed to have work, I had the font in two separate projects, so it wasn't locating them in one part initially, but after I duplicated the font this seemed to have worked. Thanks.Pantia
P
0

You need to reference the Font Awesome unicodes directly as manual implementations might result in mistake. Here is a link that would help with Font Awesome Classes: https://github.com/fzany/Font-Awesome-Cheat-Charp .

So you can do something like:

textBlock.Text = FontAwesome.Solid.Address_Book;

Here is a trim: visit the link for the full code.

public class FontAwesome
{
    public static class Solid
    {
        public static string Ad = "\uf641";
        public static string Address_Book = "\uf2b9";
        public static string Address_Card = "\uf2bb";
        public static string Adjust = "\uf042";
        public static string Air_Freshener = "\uf5d0";
        public static string Align_Center = "\uf037";
        public static string Align_Justify = "\uf039";
        public static string Align_Left = "\uf036";
        public static string Align_Right = "\uf038";
        public static string Allergies = "\uf461";
        public static string Ambulance = "\uf0f9";
        public static string American_Sign_Language_Interpreting = "\uf2a3";
        public static string Anchor = "\uf13d";
        public static string Angle_Double_Down = "\uf103";
        public static string Angle_Double_Left = "\uf100";
        public static string Angle_Double_Right = "\uf101";
        public static string Angle_Double_Up = "\uf102";
        public static string Angle_Down = "\uf107";
        public static string Angle_Left = "\uf104";
        public static string Angle_Right = "\uf105";
        public static string Angle_Up = "\uf106";
        public static string Angry = "\uf556";
        public static string Ankh = "\uf644";
        public static string Apple_Alt = "\uf5d1";
        public static string Archive = "\uf187";
        public static string Archway = "\uf557";
        public static string Arrow_Alt_Circle_Down = "\uf358";
        public static string Arrow_Alt_Circle_Left = "\uf359";
        public static string Arrow_Alt_Circle_Right = "\uf35a";
        public static string Arrow_Alt_Circle_Up = "\uf35b";
        public static string Arrow_Circle_Down = "\uf0ab";
        public static string Arrow_Circle_Left = "\uf0a8";
        public static string Arrow_Circle_Right = "\uf0a9";
        public static string Arrow_Circle_Up = "\uf0aa";
        public static string Arrow_Down = "\uf063";
        public static string Arrow_Left = "\uf060";
        public static string Arrow_Right = "\uf061";
        public static string Arrow_Up = "\uf062";
        public static string Arrows_Alt = "\uf0b2";
        public static string Arrows_Alt_H = "\uf337";
        public static string Arrows_Alt_V = "\uf338";
        public static string Assistive_Listening_Systems = "\uf2a2";
        public static string Asterisk = "\uf069";
   }
}
Puerility answered 16/10, 2018 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.