【问题标题】:Xamarin fontawesome icons displayed as square with xXamarin fontawesome 图标显示为带有 x 的正方形
【发布时间】:2020-03-07 02:07:44
【问题描述】:

按照一些教程在我的 Xamarin 项目中包含 FontAwesome 5 Free 后,它总是最终显示为一个带有 x 的正方形。

我认为这可能与我的 xamarin.forms 版本(2.3.0.46-pre3)有关,我似乎无法使其工作。

这是文件名

Build action 设置为 AndroidAsset 并复制(如果更新)和 BundleResource for IOS

我在哪里使用 Xaml

<?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:尝试使用不同的字体文件,似乎表明它可以找到字体但无法渲染它。使用以下字体渲染器得到以下结果

自定义字体渲染器

[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>

结果

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    我按照找到的说明进行操作 here 这让我开始了。还进行了一些进一步的调整以使其完全正常工作(尤其是 UWP)。我确实注意到您正在使用 .ttf 文件,而我正在使用 .otf 文件,这可能是个问题。

    我最终的最终资产文件夹和资源实现如下(android - 这里没有 IOS),希望对您有所帮助。

    <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>
    

    【讨论】:

    • 谢谢,尝试使用文件的 otf 版本,但同样的问题,是的,这是我看到的教程之一。
    • @adriano515 如果你将 Android 的哈希后的部分从 "..#Font Awesome 5 Free Solid" 调整为 "...#Regular" 我隐约记得这个问题是在处理这个属性(尽管我认为它是在 UWP 上) ,我看到我的都是Regular,而你的和你的 ttf 文件描述相匹配
    • 同样的结果,尝试在不同的字体上使用Regular并使用它们,但它甚至没有找到图标(没有x的正方形)
    【解决方案2】:

    将应用更新到 4.4 版并解决了问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      相关资源
      最近更新 更多