Xamarin Fontawesome иконки отображаются в виде квадрата с х - PullRequest
1 голос
/ 06 февраля 2020

После нескольких уроков по включению FontAwesome 5 Free в мой проект Xamarin он всегда заканчивается квадратом с х внутри.

App with the icon as square containing x

Я думаю, что это, вероятно, связано с моей версией xamarin.forms (2.3.0.46-pre3), и я не могу заставить ее работать.

Вот имена файлов

File names

Действие сборки установлено на AndroidAsset и копируется, если новее, и BundleResource для IOS

Xaml, где im их использование

<?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="&#xf011;" 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>

Возможно ли даже использование пользовательских шрифтов с моей версией? Приветствуется любая помощь

Обновление 1: обновлено до версии 2.5.1.527436, та же проблема

Обновление 2: пробовал с использованием другого файла шрифта, кажется, указывает, что может найти шрифт, но не может сделать это. Использование следующего средства визуализации шрифтов дало следующие результаты

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="&#xf011;" TextColor="White" FontFamily="{StaticResource FontAwesomeSolid}"/>
            <Label Text="&#xf011;" TextColor="White" FontFamily="{StaticResource FontAwesomeBrands}"/>
            <Label Text="&#xf011;" TextColor="White" FontFamily="{StaticResource FontAwesomeRegular}"/>
            <trainInUrl:CustomFontLabel Text="&#xf011;" FontFamily="{StaticResource FontAwesomeSolid}"/>
            <trainInUrl:CustomFontLabel Text="&#xf011;" FontFamily="{StaticResource FontAwesomeBrands}"/>
            <trainInUrl:CustomFontLabel Text="&#xf011;" FontFamily="{StaticResource FontAwesomeRegular}"/>
        </StackLayout>

Результат

enter image description here

Ответы [ 2 ]

0 голосов
/ 06 марта 2020

Обновил приложение до версии 4.4, и это решило проблему

0 голосов
/ 07 февраля 2020

Я следовал инструкциям, найденным здесь , с которых я начал. была также некоторая дальнейшая настройка, чтобы полностью заставить это работать (особенно UWP). Я отмечаю, что вы используете файлы .ttf, а я использую файлы .otf, что может быть проблемой.

Ниже приведены мои окончательная конечная папка ресурсов и реализация ресурсов (android - нет IOS здесь), надеюсь, поможет вам.

Assets

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...