Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

c# - How to use Font Awesome icons in project as an icon of ImageButton

I am having a problem to realize how to use Font Awesome icons in my Xamarin application, I want to use it with ImageButtonas icon. And most tutorials I found didn't help me understand how it works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As explained in Microsoft Documentation:

  1. You need to first to have the font file I believe .ttf (or .otf).
  2. Add it to your shared project.
  3. Right click on the file and click on "properties", then set it build action to Embedded Resource.
  4. Export it with a friendly alias name in your AssemblyInfo.cs or App.xaml.cs:

[assembly: ExportFont("file-name.ttf", Alias = "FontAwesome")]

  1. Consume it:
<Label FontFamily="FontAwesome" Text="&#xf004;"/>

For the list of icons code take a look at FontAwesome codes.


If you want to use it with click capabilities like a button, then you can use a label with GestureRecognizers:

<Label FontFamily="FontAwesome" Text="&#xf004;">
     <Label.GestureRecognizers>
         <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
     </Label.GestureRecognizers>
</Label>

UPDATE

Even better use an ImageButton with a FontImageSource property instead of a label, you have click capabilities of a button also I found interesting that you can change the color of your glyph icon, weather hard-coded or dynamically depending on the selected theme, here is an example:

<ImageButton>
    <ImageButton.Source>
        <FontImageSource FontFamily="FontAwesome"
                         Glyph="{x:Static fonts:IconFont.AddressBook}"
                         Color="{AppThemeBinding Dark=White,
                                                 Light=Black}"/>
    </ImageButton.Source>
</ImageButton>

You can also define a static class having const string properties, each one having as value the code corresponding to an icon glyph and as name a description of it that way you will need to provide only the description instead of the code like I did with Glyph="{x:Static fonts:IconFont.AddressBook}", it will looks something like this:

 static class IconFont
    {
        public const string Ad = "uf641";
        public const string AddressBook = "uf2b9";
...
}

I invite you to follow this video tutorial and check out this GitHub Page which generate the static c# class for you starting from a font glyph file that you submit.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...