Xamarin fontawesome icons displayed as square with x - xamarin

After following some tutorials to include FontAwesome 5 Free in my Xamarin project it always ends up showing as a square with an x inside.
I think this is probably related to my xamarin.forms version (2.3.0.46-pre3) and i can't seem to make it work.
Here's the file names
Build action is set to AndroidAsset and copy if newer and BundleResource for IOS
Xaml where im using them
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TrainInURL.MainPage">
<Grid >
<Image Source="bg_home.png" VerticalOptions="Start" HorizontalOptions="Center"/>
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" Margin="10" Padding="10" >
<Label Text="Bienvenid#" TextColor="White" Font="Bold,16"></Label>
<Label Text="" TextColor="White" FontFamily="{StaticResource FontAwesomeSolid}"/>
</StackLayout>
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" Margin="10" Padding="10" >
<Label x:Name="txt_Nombre" TextColor="White" Font="Bold,16"></Label>
<Label x:Name="txt_Apellido" TextColor="White" Font="Bold,16"></Label>
<Label x:Name="lblMensaje" HorizontalTextAlignment="Center"></Label>
</StackLayout>
<StackLayout >
</StackLayout>
</StackLayout>
</Grid>
</ContentPage>
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="TrainInURL.App">
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:Key="FontAwesomeSolid" x:TypeArguments="x:String"
iOS="Font Awesome 5 Free Solid"
Android="FontAwesomeSolid.ttf#Font Awesome 5 Free Solid" />
<OnPlatform x:Key="FontAwesomeRegular" x:TypeArguments="x:String"
iOS="Font Awesome 5 Free Regular"
Android="FontAwesomeRegular.ttf#Font Awesome 5 Free Regular" />
<OnPlatform x:Key="FontAwesomeBrands" x:TypeArguments="x:String"
iOS="Font Awesome 5 Free Brands"
Android="FontAwesomeBrands.ttf#Font Awesome 5 Free Brands" />
</ResourceDictionary>
<!-- Application resource dictionary -->
</Application.Resources>
</Application>
info.plist
<key>UIAppFonts</key>
<array>
<string>FontAwesomeSolid.ttf</string>
<string>FontAwesomeRegular.ttf</string>
<string>FontAwesomeBrands.ttf</string>
</array>
Is it even possible to use custom fonts with my version? Any help is welcomed
Update 1: Updated to version 2.5.1.527436, same issue
Update 2: Tried using a different font file, seems to indicate it can find the font but can't render it. Using the following font renderer gave the following results
CustomFontRenderer
[assembly: ExportRenderer(typeof(CustomFontLabel), typeof(CustomFontRenderer))]
namespace TrainInURL.Droid
{
public class CustomFontRenderer : LabelRenderer
{
public CustomFontRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
TextView label = (TextView) Control;
if (e.NewElement?.FontFamily != null)
{
Typeface font = null;
// the try-catch block will ensure the element is at least rendered with default
// system font in Xamarin Previewer instead of crashing the view
try
{
font = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, e.NewElement.FontFamily);
}
catch (Exception)
{
font = Typeface.Default;
}
label.Typeface = font;
}
}
}
}
Xaml
<StackLayout Orientation="Horizontal" Margin="10" Padding="10" >
<Label Text="Bienvenid#" TextColor="White" FontSize="16" FontAttributes="Bold"></Label>
<Label Text="" TextColor="White" FontFamily="{StaticResource FontAwesomeSolid}"/>
<Label Text="" TextColor="White" FontFamily="{StaticResource FontAwesomeBrands}"/>
<Label Text="" TextColor="White" FontFamily="{StaticResource FontAwesomeRegular}"/>
<trainInUrl:CustomFontLabel Text="" FontFamily="{StaticResource FontAwesomeSolid}"/>
<trainInUrl:CustomFontLabel Text="" FontFamily="{StaticResource FontAwesomeBrands}"/>
<trainInUrl:CustomFontLabel Text="" FontFamily="{StaticResource FontAwesomeRegular}"/>
</StackLayout>
Result

I followed the instructions found here which got me started. there was also a bit of further tweaking to completely get it working (particularly UWP). I do note you are using the .ttf files and I am using the .otf files, which could be an issue.
My final final assets folder and resource implementation are below (android - no IOS here), hopefully helps you out.
<Application.Resources>
<ResourceDictionary>
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeBrands">
<On Platform="Android" Value="FontAwesome5Brands400.otf#Regular"/>
<On Platform="UWP" Value="/Assets/Fonts/FontAwesome5Brands400.otf#Font Awesome 5 Brands"/>
</OnPlatform>
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeSolid">
<On Platform="Android" Value="FontAwesome5Solid900.otf#Regular"/>
<On Platform="UWP" Value="/Assets/Fonts/FontAwesome5Solid900.otf#Font Awesome 5 Free"/>
</OnPlatform>
<OnPlatform x:TypeArguments="x:String" x:Key="FontAwesomeRegular">
<On Platform="Android" Value="FontAwesome5Regular400.otf#Regular"/>
<On Platform="UWP" Value="/Assets/Fonts/FontAwesome5Regular400.otf#Font Awesome 5 Free"/>
</OnPlatform>
</ResourceDictionary>
</Application.Resources>

Updated the app to version 4.4 and that solved the issue

Related

why click events or gestures are not work in carousel page childs on IOS?

Im trying to click Image,box or label object which in Carousel views content page. Android works fine but IOS not, why? Can you help or explain?
Here is simple demo:
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CarouselPageNavigation.MainPage">
<ContentPage>
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS, Android" Value="0,40,0,0" />
</OnPlatform>
</ContentPage.Padding>
<StackLayout>
<Label Text="Green" FontSize="Medium" HorizontalOptions="Center" />
<BoxView Color="Green" WidthRequest="200" HeightRequest="200" HorizontalOptions="Center" VerticalOptions="CenterAndExpand">
<BoxView.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
</BoxView.GestureRecognizers>
</BoxView>
</StackLayout>
</ContentPage>
Here is .cs
private void TapGestureRecognizer_Tapped(object sender, System.EventArgs e)
{
DisplayAlert("sdada", "sdada", "sdadada");
}
I solved it update to beta IOS SDK update on mac side before xamarin.forms 4.5.0.617.
Now this issue has been fixed , you can update Xamrin Forms to the latest version 4.5.0.617 , then it will work in iOS 13.4 .
Thanks Junior Jiang

Xamarin Forms NavigationPage.TitleView RTL, the content disappered

I have a xamarin.forms app that supports RTL, but when i convert a Page to RTL the NavigationPage.TitleView text disapears.
The NavigationPage.TitleView Code:
<NavigationPage.TitleView>
<Label Text="{Binding Title}" Style="{StaticResource TittleLabel}" HorizontalTextAlignment="Start" HorizontalOptions="Start" VerticalOptions="CenterAndExpand"></Label>
</NavigationPage.TitleView>
This is the Result screen.
Thanks in Advance.
For RTL, the Label having RTL issues. Please find the issue link below.
https://github.com/xamarin/Xamarin.Forms/issues/3611
When setting RTL to Page, the title view is disappeared. you can report to the Xamarin team. For instead of setting a title through Label, directly setting title property of ContentPage like below,
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="RTL"
FlowDirection="RightToLeft">
It's working but RTL is not applying. But the title is displayed. You can report this issue also.
I guess maybe the binding do not work. I make a sample for your reference.
MainPage.xaml
<StackLayout>
<Button Clicked="Button_Clicked" Text="Title View Page" />
</StackLayout>
MainPage.xaml.cs
private void Button_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new TitleViewPage());
}
TitleViewPage.xaml
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="TittleLabel" TargetType="Label">
<Setter Property="TextColor" Value="Green" />
<Setter Property="FontSize" Value="Large" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<NavigationPage.TitleView>
<StackLayout>
<Label
HorizontalOptions="Start"
HorizontalTextAlignment="Start"
Style="{StaticResource TittleLabel}"
Text="{Binding Title}"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</NavigationPage.TitleView>
<ContentPage.Content>
<StackLayout>
<Label
HorizontalOptions="CenterAndExpand"
Text="Welcome to TitleView Page!"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
TitleViewPage.xaml.cs
public string Title { get; set; }
public TitleViewPage()
{
InitializeComponent();
Title = "My TitleView";
this.BindingContext = this;
}
App.xaml.cs
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
Updated:
When you set the FlowDirection to RightToLeft, the titleview could be seen on horizontal screen.
On the Right-to-left localization document, it lists the limitations.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/right-to-left
NavigationPage button location, toolbar item location, and transition animation is controlled by the device locale, rather than the FlowDirection property.
This question has also be reported. You could follow the update . https://github.com/xamarin/Xamarin.Forms/issues/9083
this aproach worked for me:
-set RTL-FlowDirection for main-layout (wrapper) of ContentPage's content (instead of ContentPage)
App.xaml
<Application.Resources>
<ResourceDictionary>
<Style x:Key="PageTitleStyle" TargetType="Label">
<Setter Property="HorizontalOptions" Value="EndAndExpand"/>
<Setter Property="Padding" Value="0,0,5,0"/>
</Style>
</ResourceDictionary>
</Application.Resources>
MainPage.xaml
<ContentPage ....>
<NavigationPage.TitleView >
<Label Text="{Binding Title}" Style="{StaticResource PageTitleStyle}" />
</NavigationPage.TitleView>
<StackLayout FlowDirection="RightToLeft">
....
</StackLayout>
</ContentPage>

Xamarin Forms - center title in a stacklayout

The screenshot below shows a header that includes a hamburger menu and a title. Notice how the title is centered on it's bounding area (the red box). How can I get the title to center on the width of the phone, but leave the hamburger menu where it is?
Here is the code that creates the header area...
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WP.MobileMidstream.Device.Pages.RunSheetHeader">
<ContentView.Resources>
<Style x:Key="HeaderStyle" TargetType="StackLayout">
<Setter Property="BackgroundColor" Value="#00458C" />
</Style>
</ContentView.Resources>
<ContentView.Content>
<StackLayout Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
Style="{StaticResource HeaderStyle}">
<StackLayout.HeightRequest>
<OnPlatform x:TypeArguments="x:Double">
<On Platform="iOS">80</On>
<On Platform="Android">56</On>
</OnPlatform>
</StackLayout.HeightRequest>
<Image Source="hamburger_icon"
Margin="10" />
<Label VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"
HorizontalOptions="FillAndExpand"
FontSize="20"
BackgroundColor="Red"
TextColor="White">Daily Run Sheet</Label>
</StackLayout>
</ContentView.Content>
</ContentView>
#Jason suggested a Grid instead of the StackLayout. Here is what I came up with that works. Thanks #Jason!
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WP.MobileMidstream.Device.Pages.RunSheetHeader">
<ContentView.Resources>
<Style x:Key="HeaderStyle" TargetType="Grid">
<Setter Property="BackgroundColor" Value="#00458C" />
</Style>
</ContentView.Resources>
<ContentView.Content>
<Grid Style="{StaticResource HeaderStyle}">
<Grid.RowDefinitions>
<RowDefinition>
<RowDefinition.Height>
<OnPlatform x:TypeArguments="GridLength">
<On Platform="iOS">80</On>
<On Platform="Android">56</On>
</OnPlatform>
</RowDefinition.Height>
</RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="2"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"
HorizontalOptions="Fill"
FontSize="20"
TextColor="White">Daily Run Sheet</Label>
<Image Source="hamburger_icon"
Grid.Row="0"
Grid.Column="0"
Margin="10" />
</Grid>
</ContentView.Content>
</ContentView>

Xamarin change font in XAML

How can I change the font of a label in Xamarin.Forms XAML?
Predictions are not available for font family attribute in XAML.
I tried 2 or 3 fonts like 'Arial' in font family, but it's not working.
Help me to resolve this.
<StackLayout Orientation="Vertical">
<Label Text="Welcome !"
VerticalOptions="Start"
HorizontalOptions="StartAndExpand"
FontFamily="Arial"
/>
</StackLayout>
store ttf file Location
Android: YouNameSpace.Droid/Assets/Poppins-Light.ttf
Ios: YouNameSpace.iOS/Resources/fonts/Poppins-Light.ttf
for iOS Need to put Below line in info.plist file
create static resource in App.xaml
<Application.Resources>
<ResourceDictionary>
<!-- FontFamily -->
<OnPlatform x:Key="PoppinsBold"
x:TypeArguments="x:String"
iOS="Poppins-Bold"
Android="Poppins-Bold.ttf#Poppins"/>
<OnPlatform x:Key="PoppinsLight"
x:TypeArguments="x:String"
iOS="Poppins-Light"
Android="Poppins-Light.ttf#Poppins" />
<!-- style for Lable -->
<Style x:Key="PoppinsBoldLabelStyle"
TargetType="{x:Type Label}">
<Setter Property="FontFamily"
Value="{StaticResource PoppinsBold}" />
</Style>
<Style x:Key="PoppinsLightLabelStyle"
TargetType="{x:Type Label}">
<Setter Property="FontFamily"
Value="{StaticResource PoppinsLight}" />
</Style>
</ResourceDictionary>
</Application.Resources>
use static resource in Lable
<Label Text="Hello" Style="{StaticResource PoppinsBoldLabelStyle}"/>
I resolved it by copying the font in 'Assets' folder for Android and changed the code as following:
<StackLayout Orientation="Vertical">
<Label Text="Hello Forms with XAML" FontFamily="ariblk.ttf#ariblk"/>
<Label Text="New Line following the first line " FontFamily="chiller.ttf#chiller" />
</StackLayout>
Please refer to this, and here is official demo.
Or you can use Custom renderers.
Here download the Arial, and put your Assets/Fonts folder.
MyLabel in your PCL:
namespace FontTes
{
public class MyLabel:Label
{
}
}
MyLabelRender in your Android platform:
using Android.Content;
using Android.Graphics;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(FontTes.MyLabel), typeof(FontTes.Droid.MyLabelRender))]
namespace FontTes.Droid
{
class MyLabelRender: LabelRenderer
{
public MyLabelRender(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
var label = (TextView)Control;
label.Typeface = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, "Fonts/arial.ttf");
}
}
}
PCL's MainPage.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:FontTes;assembly=FontTes"
x:Class="FontTes.MainPage">
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<local:MyLabel
Text="Welcome to Xamarin.Forms!"
VerticalOptions="Center"
HorizontalOptions="Center"/>
</StackLayout>
</ContentPage>

Multiple platform condition in XAML view - Xamarin forms [duplicate]

I have the following C# code:
var footer = new StackLayout()
{ BackgroundColor = Device.OnPlatform(Color.FromRgb(225, 240, 251), Color.FromRgb(225, 240, 251), Color.Black),
};
How can I translate that into Xamarin Xaml, more importantly the Device Platform specifics for the FromRgb?
I have the following XAML so far... again, it's the FromRgb that's stumping me.
<StackLayout.BackgroundColor>
<Color>
<OnPlatform x:TypeArguments="Color"></OnPlatform>
</Color>
</StackLayout.BackgroundColor>
UPDATE 14/02/2015
I've just tried the answer below and I have the following but it's not updating the background colour for either iOS or Windows Phone. If I add the Background Color to the root StackLayout element it works...:
<StackLayout HorizontalOptions="FillAndExpand">
<StackLayout.BackgroundColor>
<Color>
<OnPlatform x:TypeArguments="Color">
<OnPlatform.WinPhone>#51C0D4</OnPlatform.WinPhone>
<OnPlatform.iOS>#51C0D4</OnPlatform.iOS>
</OnPlatform>
</Color>
</StackLayout.BackgroundColor>
<Label Text=""></Label>
<StackLayout Orientation="Horizontal" VerticalOptions="EndAndExpand" HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Button Text="Login"></Button>
<Button Text="Sign Up"></Button>
</StackLayout>
</StackLayout>
</StackLayout>
You're almost there. The default converter takes care of converting the color from either a named color (e.g. White, Red, etc.) or a hex color (e.g.: #FF0000).
<StackLayout.BackgroundColor>
<OnPlatform x:TypeArguments="Color">
<OnPlatform.iOS>#FF0000</OnPlatform.iOS>
<OnPlatform.Android>#00FF00</OnPlatform.Android>
</OnPlatform>
</StackLayout.BackgroundColor>
Newer version syntax of OnPlatform is slightly different
In Xaml:
<ResourceDictionary>
<OnPlatform x:Key="SwitchOnColor" x:TypeArguments="Color" >
<On Platform="iOS|Android" >#0000FF</On>
<On Platform="UWP">#FF0000</On>
</OnPlatform>
</ResourceDictionary>
In code:
switch (Device.RuntimePlatform)
{
case "iOS":
break;
case "Android":
break;
case "UWP":
break;
default:
break;
}
You can do this also:
<ContentView>
<OnPlatform x:TypeArguments="View">
<On Platform="iOS">
<Label Text="iOS" />
</On>
<On Platform="Android">
<Label Text="Android" />
</On>
</OnPlatform>
</ContentView>

Resources