How to use Font Awesome 5 Duotone Xamarin Forms - xamarin

I'm trying to apply Font Awesome 5 Duotone font family to my Xamarin Forms app but when launching it, icon is not displaying as it should...
<OnPlatform x:Key="FontAwesomeFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="fa-light-300.ttf#Font Awesome 5 Pro" />
<On Platform="iOS" Value="Font Awesome 5 Pro" />
<On Platform="UWP" Value="/Assets/Fonts/fa-light-300.ttf#Font Awesome 5 Pro" />
</OnPlatform>
<OnPlatform x:Key="FontAwesomeDuotoneFontFamily" x:TypeArguments="x:String">
<On Platform="Android" Value="fa-duotone-900.ttf#Font Awesome 5 Duotone" />
<On Platform="iOS" Value="Font Awesome 5 Pro Duotone Solid" />
<On Platform="UWP" Value="/Assets/Fonts/fa-duotone-900.ttf#Font Awesome 5 Duotone" />
</OnPlatform>
In the code, FontAwesome Light works as expected. Am I doing anything wrong?

Related

How to use an icon from an embedded resource font file in Xamarin Forms

I want to use icons from Font Awesome in my Xamarin Forms application. I have followed several tutorials I found but I still can't get the icons to display. Here is a portion of the static class showing the class name and namespace:
namespace NGIC_XAML.Constants
{
public static class IconFontsFAProRegular
{
public const string GlassMartini = "\uf000";
public const string Music = "\uf001";
public const string Search = "\uf002";
public const string Heart = "\uf004";
}
}
Here is the ExportFont statement in my AssemblyInfo.cs file:
[assembly: ExportFont("Font-Awesome-5-Pro-Regular-400.otf", Alias = "FAProRegular")]
Here is the declaration of the namespace in my XAML file:
xmlns:local="clr-namespace:NGIC_XAML.Constants"
And finally, here is the tag where I want to use one of the font icons:
<Image
HeightRequest="44"
HorizontalOptions="Center"
VerticalOptions="Center"
WidthRequest="44">
<Image.Source>
<FontImageSource
FontFamily="FAProRegular"
Glyph="{x:Static local:IconFontsFAProRegular.Heart}"
Size="44"
Color="{StaticResource NGIC_Red}" />
</Image.Source>
</Image>
The output
Someone is bound to ask, so here are the packages installed:
I would sure like to know what I;m doing wrong! I also don't get intellisense when I enter the namespace name in the ImageSource.
You must set font family as static resource and reference it in your code. Otherwise it will not work.
<Image.Source>
<FontImageSource
FontFamily="{StaticResource FontAwesomeRegular}"
Glyph="{x:Static local:IconFontsFAProRegular.Heart}"
Size="44"
Color="{StaticResource NGIC_Red}" />
Example:
<OnPlatform x:Key="FontAwesomeBrands" x:TypeArguments="x:String">
<On Platform="Android" Value="FontAwesome5Brands.otf#Regular" />
<On Platform="iOS" Value="FontAwesome5Brands-Regular" />
<On Platform="UWP" Value="/Assets/FontAwesome5Brands.otf#Font Awesome 5 Brands" />
</OnPlatform>
<OnPlatform x:Key="FontAwesomeSolid" x:TypeArguments="x:String">
<On Platform="Android" Value="FontAwesome5Solid.otf#Regular" />
<On Platform="iOS" Value="FontAwesome5Free-Solid" />
<On Platform="UWP" Value="/Assets/FontAwesome5Solid.otf#Font Awesome 5 Free" />
</OnPlatform>
<OnPlatform x:Key="FontAwesomeRegular" x:TypeArguments="x:String">
<On Platform="Android" Value="FontAwesome5Regular.otf#Regular" />
<On Platform="iOS" Value="FontAwesome5Free-Regular" />
<On Platform="UWP" Value="/Assets/FontAwesome5Regular.otf#Font Awesome 5 Free" />
</OnPlatform>
When using C# code in a XAML file it is important to add the assembly, with this you can use your Font Icons without any problem.
xmlns:local="clr-namespace:NGIC_XAML.Constants;assembly=NGIC_XAML"

Unable to read double from application resource within the viewmodel class

I have defined a resource in App.xaml which am trying to read from a view model class.
Different versions which were tried
<OnPlatform x:Key="HandpickedPhone"
x:TypeArguments="x:Double"
Android="250"
iOS="240" />
<OnPlatform x:Key="HandpickedTablet"
x:TypeArguments="x:Double"
Android="375"
iOS="360" />
<OnIdiom x:Key="HandpickedHeight"
x:TypeArguments="x:Double"
Phone="{StaticResource HandpickedPhone}"
Tablet="{StaticResource HandpickedTablet}" />
<OnPlatform x:Key="HorizontalListHeight"
x:TypeArguments="x:Double">
<OnPlatform.Android>
<OnIdiom x:TypeArguments="x:Double"
Phone="250"
Tablet="375" />
</OnPlatform.Android>
<OnPlatform.iOS>
<OnIdiom x:TypeArguments="x:Double"
Phone="240"
Tablet="260" />
</OnPlatform.iOS>
</OnPlatform>
I am trying to read it and set the height request dynamically like,
double height = (double)App.Current.Resources["HorizontalListHeight"];
horizontalStack.HeightRequest = height;
But casting to double throws IllegalCastException, and at the same time, I am able to cast into the correct value from the watcher in VS.
You have to use Width or Height or Thickness in x:TypeArguments
Like this:-
<OnPlatform x:TypeArguments="Thickness">
<OnPlatform.Platforms>
<On Platform="iOS" Value="0, 20, 0, 0" />
<On Platform="Android" Value="0, 0, 0, 0" />
<On Platform="UWP" Value="0, 0, 0, 0" />
</OnPlatform.Platforms>
</OnPlatform>
First , check https://forums.xamarin.com/discussion/comment/354754/#Comment_354754.
Here height is not double , it is OnPlatform<double> , so modify your code as
var height = (OnPlatform<double>)App.Current.Resources["HorizontalListHeight"];
horizontalStack.HeightRequest = height;
However , it's not the recommended way , we often use Style in ResourceDictionaty.
<Style x:Name="HorizontalListHeight" TargetType="StackLayout">
<Setter Property="HeightRequest">
<Setter.Value>
<OnPlatform x:TypeArguments="x:Double">
<OnPlatform.Android>
<OnIdiom x:TypeArguments="x:Double" Phone="250" Tablet="375" />
</OnPlatform.Android>
<OnPlatform.iOS>
<OnIdiom x:TypeArguments="x:Double" Phone="240" Tablet="260" />
</OnPlatform.iOS>
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
horizontalStack.Style = Application.Current.Resources["HorizontalListHeight"] as Style;

How can I add on-platform parameter into a Xamarin Label?

Here's the code I have:
<?xml version="1.0" encoding="utf-8"?>
    <Label xmlns="http://xamarin.com/schemas/2014/forms"
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
           xmlns:local="clr-namespace:Japanese;assembly=Japanese"
           x:Class="Japanese.Templates.MessageLabel"
           FontSize="{DynamicResource MessageTextFontSize}"
           TextColor="{DynamicResource FooterTextColor}" />
I would like to make it so that the Property="FontFamily" is set like this:
       
    
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="FontAwesome5ProLight" />
   <On Platform="Android" Value="Font Awesome 5 Pro-Light-300.otf#FontAwesome5ProLight" />
</OnPlatform>
but how can I make this into a parameter?
Like this, you can try. The platform syntax has changed in Xamarin.Forms 3.2 (I guess),
<Label xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.MessageLabel"
FontSize="{DynamicResource MessageTextFontSize}"
TextColor="{DynamicResource FooterTextColor}">
<Label.FontFamily>
<OnPlatform
x:TypeArguments="x:String"
Android="Font Awesome 5 Pro-Light-300.otf#FontAwesome5ProLight"
iOS="FontAwesome5ProLight" />
</Label.FontFamily>
</Label>
but the old syntax(mentioned in the question) will work as well if you want to use that.
<Label xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.MessageLabel"
FontSize="{DynamicResource MessageTextFontSize}"
TextColor="{DynamicResource FooterTextColor}">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="FontAwesome5ProLight" />
<On Platform="Android" Value="Font Awesome 5 Pro-Light-300.otf#FontAwesome5ProLight" />
</OnPlatform>
</Label.FontFamily>
</Label>
I'm not sure I understood your question properly, but is that what you are looking for ?
for better use define font family in style and apply wherever you want
<OnPlatform x:TypeArguments="x:String" x:Key="MediumFont">
<On Platform="Android" Value="fonts/HKGrotesk_Medium.ttf#HK Grotesk" />
<On Platform="UWP" Value="/Assets/Fonts/HKGrotesk_Medium.ttf#HK Grotesk" />
<On Platform="iOS" Value="HKGrotesk-Medium" />
</OnPlatform>
Add This In App.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Auction.App">
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:TypeArguments="x:String"
Android="FontAwesome5ProLight.otf#FontAwesome5ProLight"
iOS="FontAwesome5ProLight"
WinPhone="20"
x:Key="FontFamilyName" />
<Style x:Key="labelStyle" TargetType="Label">
<Setter Property="FontFamily" Value="{DynamicResource FontFamilyName}"/>
<Setter Property="FontSize" Value="12" />
<Setter Property="VerticalTextAlignment" Value="Center"/>
<Setter Property="TextColor" Value="#000000"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
Use in Any Page Of Your App
in Xaml
<Label x:Name="Register" Text="Registeration" HorizontalTextAlignment ="Center" FontSize="20" Style="{StaticResource labelStyle}">
<Label.GestureRecognizers >
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" ></TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
and cs
label.Style = (Style)Application.Current.Resources["labelStyle"];

Invalid type: expected type is BindableProperty

I'm trying to set the Padding based on the platform with the following code
<ContentView.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS">16,24,16,4</On>
<On Platform="Windows">16,16,16,4</On>
<On Platform="Android">16,16,16,4</On>
</OnPlatform>
</ContentView.Padding>
However, I'm getting a red squiggly on OnPlatform with the following error
Invalid type: expected type is BindableProperty, actual type is OnPlatform<Thickness>
What's the correct way to configure this?
This works fine on mine, also running 2.3.4. If it doesn't work on yours you will need to show more code, perhaps doing something weird in the code behind.
<ContentView.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="16,24,16,4"></On>
<On Platform="Windows" Value="16,16,16,4"></On>
<On Platform="Android" Value="16,16,16,4"></On>
</OnPlatform>
</ContentView.Padding>
`<ContentView>
<ContentView.Padding>
<OnPlatform x:TypeArguments="Thickness"
Android="16,16,16,4"
WinPhone="16,16,16,4"
iOS="16,24,16,4"/>
</ContentView.Padding>
<ContentView>`
This worked for me

Xamarin Forms Fixed Size for an image

I'm trying to set the height and width of my image and have it a fixed size however if I shrink the container (frame, stackpanel, grid, etc...) the image shrinks along with it. However I would have assumed setting the height and width (WidthRequest, HeightRequest, MinimumHeightRequest, MinimumWidthRequest) would have forced the image to remain the set size... however it doesn't/
What can I do...
Try this; This will set the width and height of the image with the platform specific dimensions i.e. it will use dp for android and points for iOS.
<Image IsVisible="true">
<Image.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="72" Android="100" WinPhone="10" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="212" Android="140" WinPhone="10" />
</OnIdiom.Tablet>
</OnIdiom>
</Image.HeightRequest>
<Image.WidthRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="125" Android="150" WinPhone="10" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="265" Android="190" WinPhone="10" />
</OnIdiom.Tablet>
</OnIdiom>
</Image.WidthRequest>
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<OnPlatform.iOS>
<FileImageSource File="logo.png" />
</OnPlatform.iOS>
<OnPlatform.Android>
<FileImageSource File="logo.png" />
</OnPlatform.Android>
<OnPlatform.WinPhone>
<FileImageSource File="logo.png" />
</OnPlatform.WinPhone>
</OnPlatform>
</Image.Source>
</Image>

Resources