Reputation: 3560
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"></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("");
and the following which returns the literal string:
textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome");
textBlock.Text = "";
Any ideas?
Upvotes: 9
Views: 5047
Reputation: 1090
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";
}
}
Upvotes: 0
Reputation: 646
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);
Upvotes: 7