xamarin.forms - FontAwesome 标签渲染器 iOS 麻烦
问题描述
我试图弄清楚如何使这个 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");
}
}
}
}
有问题的部分是这样的:
Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets,
"Fonts/" + FontAwesomeLabel.FontAwesomeName + ".ttf");
Control.Typeface 或只是 Typeface 给出了一些问题(红色下划线)。
iOS中的字体是什么?
希望有人可以帮助并提前感谢:-)
解决方案
只需Style
为您想要具有此字体类型的标签定义一个。您可以在 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>
确保将 *.otf(或 *.ttf)字体文件添加到Resources
文件夹中,并确保在info.plist
文件中添加特定键,如下所示:
<key>UIAppFonts</key>
<array>
<string>Fonts/FontAwesome.otf</string>
</array>
注意:在我的例子中,字体文件位于 Resources/Fonts/ 目录中
编辑:设置时实际发生的FontFamily
事情与您在渲染器中所做的事情完全相同。签出Xamarin.Forms github 源UpdateFont
中的方法和 LabelRenderer
在其中调用的方法。ToTypeFace
推荐阅读
- android - 对长按执行波纹效果
- reactjs - 如何查找在特定时间范围内单击按钮的次数(0.5秒内)
- html - 使用 VBA 在导出的 HTML 表中添加超链接
- tfs - 由于重复的 ProjectGuid,没有对 dotnet Core 项目进行 SonarQube 分析
- javascript - 使用 div id 遍历列表
- error-handling - 向订阅 RXJS 发送错误
- java - 将长列表转换为字符串集
- azure - 执行 DownloadToFIleParallelAsync 方法时的问题
- java - Java:确定两个正则表达式模式是否重叠
- node.js - 带有heroku的axios