Xamarin表单UIBarButtonItem自定义字体

时间:2018-08-18 02:43:37

标签: xamarin xamarin.forms xamarin.ios custom-renderer

我试图在Xamarin Forms项目的NavBar中的UIBarButtonItem中使用FontAwesome字体。

我的XAML代码

<TabbedPage.ToolbarItems>
    <ToolbarItem Command="{Binding CreateNewConversation}" Text="&#xf067;" x:Name="NewConversationButton"></ToolbarItem>
</TabbedPage.ToolbarItems>

在ViewWillAppear内部调用的我的CustomRenderer.cs代码

var _font = UIFont.FromName("Font Awesome 5 Free", 18f);

var rightBarButtonTextAttributes = new UITextAttributes
{
    Font = _font,
    TextColor = UIColor.FromRGB(255, 194, 32),
    TextShadowColor = UIColor.Clear
};

this.NavigationController.TopViewController.NavigationItem.RightBarButtonItem.SetTitleTextAttributes(rightBarButtonTextAttributes, UIControlState.Normal);

我可以看到属性已被应用,因为我可以看到文本更新的颜色,但是我得到了?周围有方形边框。我设置了一个断点,并以正确的字体设置了_font。有什么明显的证据表明我做错了吗?

更新:

问题是缺少对UIBarButtonItem施加粗体字体粗细的方法,我仍然不确定该怎么做。我可以使用其他图标,但是每当更改选项卡时,图标都会回到围绕?的正方形。并丢失其TintColor。

1 个答案:

答案 0 :(得分:1)

再次检查您是否使用了正确的图标代码&#xf067;,以及是否在字体中可用。

由于没有出现任何错误,因此应该找到字体并正确注册字体。看来iOS无法在字体文件中找到此特定字符代码。