How to Use Font Awesome Icons in Project as an Icon of Imagebutton

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

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=""/>

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="">
<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.

How to use FontAwesome icons in asp.net webform ImageButton controls

As FontAwesome is a font that requires HTML elements to display, neither <asp:ImageButton /> (only allows images) nor <asp:Button /> (only allows plain text) are suitable. You are better off using a HtmlButton.

HtmlButton Remarks: The HTML 4.0 <button> element enables you to create buttons composed of embedded HTML elements (and even other Web Forms controls).

<button id="btnSearch" runat="server"><i class="fa fa-search"></i> Search</button>

Read more: HtmlButton Control

Xamarin Stack icon

The code you provided is used to load the font from font file. You could refer to the steps in the link below. https://stackoverflow.com/a/65095438/11850033

It need to download the Font Awesome 4.7.0 first form github.

If you want to load the icon from html, you could use webview.

var browser = new WebView();
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = @"<html><body>
<h1>Xamarin.Forms</h1>
<p>Welcome to WebView.</p>
</body></html>";
browser.Source = htmlSource;

For more details about how to load html, you could check the link below.https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows#html-strings

put awesome font icons in c # code behind

You don't need to create a new ContentPage, just create a FontImageSource object and set the values accordingly then pass this object to the IconImageSource of your contenido Page.

Also, you will need to get the StaticResource from the App Resources Dictionary.

Something like this should work:

var solidFontFamily = Application.Current.Resources["Solid"] as OnPlatform<string>;
contenido.IconImageSource = new FontImageSource() { FontFamily = solidFontFamily, Glyph= "\uf108" };

Hope this helps.-

How to change font awesome icon with click event?

For AppThemeBinding it seems not to be supported from the code till now Ability to set AppThemeBinding on Styles in code, you can test Application.Current.UserAppTheme one time but it won't be dynamic.

using System.Linq;
...

private void Button_OnPressed(object sender, EventArgs e)
{
Application.Current.Resources.TryGetValue("FontAwesomeRegular", out object fontFamily);

if (fontFamily == null)
//this should not occurred, throw an exception

string fontFamilyString = Device.RuntimePlatform switch
{
Device.Android => (fontFamily as OnPlatform<string>).Platforms.Where(x => x.Platform[0].Equals(Device.Android)).Select(x => (string)x.Value).FirstOrDefault(),
Device.iOS => (fontFamily as OnPlatform<string>).Platforms.Where(x => x.Platform[0].Equals(Device.iOS)).Select(x => (string)x.Value).FirstOrDefault(),
Device.UWP => (fontFamily as OnPlatform<string>).Platforms.Where(x => x.Platform[0].Equals(Device.UWP)).Select(x => (string)x.Value).FirstOrDefault(),
_ => string.Empty
};

if (ButtonBackColor == true)
{
stopWatch.Source = new FontImageSource()
{
FontFamily= fontFamilyString,
Glyph = FontAwesomeIcons.ArrowAltCircleDown,
Color = Application.Current.UserAppTheme == OSAppTheme.Dark
? Color.Green
: Color.White;
};
}
else
{
stopWatch.Source = new FontImageSource()
{
FontFamily= fontFamilyString,
Glyph = FontAwesomeIcons.ArrowAltCircleUp,
Color = Application.Current.UserAppTheme == OSAppTheme.Dark
? Color.Green
: Color.White;
};
}

ButtonBackColor = !ButtonBackColor;
}

Xamarin FontAwesome icons seen as squares with an x inside

I think what missing is that you should include the files extension in the ExportFont:

using Xamarin.Forms.Xaml;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
[assembly: ExportFont("Font Awesome 5 Brands-Regular-400.ttf", Alias = "FAB")]
[assembly: ExportFont("Font Awesome 5 Free-Regular-400.ttf", Alias = "FAR")]
[assembly: ExportFont("Font Awesome 5 Free-Solid-900.tff", Alias = "FAS")]

More details How to use Font Awesome icons in project as an icon of ImageButton

How to use icons and symbols from Font Awesome on Native Android Application

Font Awesome seems to be working fine for me in my android app. I did the following:

  1. Copied fontawesome-webfont.ttf into my assests folder
  2. Found the character entities for icons I wanted, using this page: http://fortawesome.github.io/Font-Awesome/cheatsheet/
  3. Created an entry in strings.xml for each icon. Eg for a heart:

    <string name="icon_heart"></string>
  4. Referenced said entry in the view of my xml layout:

     <Button
    android:id="@+id/like"
    style="?android:attr/buttonStyleSmall"
    ...
    android:text="@string/icon_heart" />
  5. Loaded the font in my onCreate method and set it for the appropriate Views:

    Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
    ...
    Button button = (Button)findViewById( R.id.like );
    button.setTypeface(font);


Related Topics



Leave a reply



Submit