Xamarin.forms Fontios的麻烦

Xamarin.forms Fontios的麻烦,xamarin.forms,xamarin.ios,font-awesome,Xamarin.forms,Xamarin.ios,Font Awesome,我正试图找出如何让这个Android示例与iOS一起工作: using System; using md.UserControls; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; namespace md.iOS.CustomRenderers { public class FontAwesomeLabelRenderer : LabelRenderer { protected override v

我正试图找出如何让这个Android示例与iOS一起工作:

using System;
using md.UserControls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

namespace md.iOS.CustomRenderers
{
    public class FontAwesomeLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets,
                    "Fonts/" + FontAwesomeLabel.FontAwesomeName + ".ttf");
            }
        }
    }
}
控件。字体或仅字体给出了一些问题(红色下划线)

iOS的字体是什么

希望有人能提前帮助和感谢:-)

我建议您使用,而不是编写自己的自定义渲染器

这是一个简单的插件,可以让你使用很多图标字体(比如Fontawesome)

App.xaml.cs(插件初始化):

Iconize.With(new FontAwesomeModule());
xmlns:iconize="clr-namespace:Plugin.Iconize;assembly=Plugin.Iconize"

<iconize:IconLabel Text="fa-info" />
new IconLabel
{
    Text = "fa-info",
    FontSize = 55
};
初始化后,您可以这样使用它:

XAML页面:

Iconize.With(new FontAwesomeModule());
xmlns:iconize="clr-namespace:Plugin.Iconize;assembly=Plugin.Iconize"

<iconize:IconLabel Text="fa-info" />
new IconLabel
{
    Text = "fa-info",
    FontSize = 55
};

只需为要使用此字体类型的标签定义一个
样式
。您可以在XAML中定义
FontFamily
,如下所示:

<Style x:Key="NameOfYourLabelStyle" TargetType="Label">
    <Setter Property="FontFamily">
        <OnPlatform x:TypeArguments="x:String">
            <On Platform="Android">Fonts/FontAwesome.otf#FontAwesome</On>
            <On Platform="UWP">/Assets/Fonts/FontAwesome.otf#FontAwesome</On>
            <On Platform="iOS">FontAwesome</On>
        </OnPlatform>
    </Setter>
</Style>    
注意:在我的例子中,字体文件位于Resources/Fonts/目录中


编辑:设置
FontFamily
时,引擎盖下实际发生的情况与渲染器中的情况完全相同。查看中的
LabelRenderer
中的
UpdateFont
方法,并在其中调用该方法。

我已尝试实现该方法,但存在一些问题。如您所述添加了第一行,但出现错误:new FontAwesomeModule()。您对此有何想法?@Mansa如果要使用FontAwesome,您还需要此软件包:确定,尝试安装时出现此错误:无法添加xam.plugin.iconize.FontAwesome?可能这是错误:检测到包降级:Xam.Plugin.Iconize从2.0.0.37-beta降级到1.0.10。直接从项目中引用包以选择其他版本。md(>=1.0.0)->Xam.Plugin.Iconize.fontawome(>=2.0.0.37-beta)->Xam.Plugin.Iconize(>=2.0.0.37-beta)md(>=1.0.0)->Xam.Plugin.Iconize(>=1.0.10)这两个软件包的版本号都应该是:2.0.0.0.37-beta。您可能需要在NuGet软件包管理器中启用预览版本。好的,现在两个软件包都已安装,但我仍然看到下面的红线:new FontAwesomeModule()?怎么办?