How to use Font Awesome icons in project as an icon of ImageButton
As explained in Microsoft Documentation:
- You need to first to have the font file I believe
.ttf
(or .otf). - Add it to your shared project.
- Right click on the file and click on "properties", then set it build action to Embedded Resource.
- Export it with a friendly alias name in your
AssemblyInfo.cs
orApp.xaml.cs
:
[assembly: ExportFont("file-name.ttf", Alias = "FontAwesome")]
- 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:
- Copied
fontawesome-webfont.ttf
into my assests folder - Found the character entities for icons I wanted, using this page: http://fortawesome.github.io/Font-Awesome/cheatsheet/
Created an entry in strings.xml for each icon. Eg for a heart:
<string name="icon_heart"></string>
Referenced said entry in the view of my xml layout:
<Button
android:id="@+id/like"
style="?android:attr/buttonStyleSmall"
...
android:text="@string/icon_heart" />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
How to Write a Unit Test to Determine Whether an Object Can Be Garbage Collected
JSON Serializer Object with Internal Properties
Getting Time Span Between Two Times in C#
Difference Between the System.Array.Copyto() and System.Array.Clone()
Why Are Tolookup and Groupby Different
Compare Compiled .Net Assemblies
ASP.NET Core Identity - Get Current User
Calling Async Method on Button Click
Suppress Null Value Types from Being Emitted by Xmlserializer
Best Way to Share Data Between Two Child Components in Blazor
Recommend a C# Task Scheduling Library
Importing Excel into a Datatable Quickly
How to Define Properties in Partial Classes, Then Mark Them with Attributes in Another Partial Class
Using Iconfiguration in C# Class Library
How to Do Tostring for a Possibly Null Object
Run a Background Task from a Controller Action in ASP.NET Core