Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 表单自定义字体_Ios_Fonts_Xamarin.forms_Custom Font - Fatal编程技术网

Ios 表单自定义字体

Ios 表单自定义字体,ios,fonts,xamarin.forms,custom-font,Ios,Fonts,Xamarin.forms,Custom Font,我正在尝试在我的Xamarin表单应用程序中实现自定义字体。字体是谷歌的流沙字体 检查表: 字体已在不同的位置添加到Fonts/、根目录和资源文件夹。 构建操作被设置为BundleResource,并且输出控制器已经使用Always Copy和Do Not Copy进行了测试。 Plist已修订为 UIAppFonts Quicksand-Regular.tff 字体的可用名称只是Quicksand,但我尝试在应用程序中同时使用Quicksand和Quicksand Regular引用它。 我想

我正在尝试在我的Xamarin表单应用程序中实现自定义字体。字体是谷歌的流沙字体

检查表: 字体已在不同的位置添加到Fonts/、根目录和资源文件夹。 构建操作被设置为BundleResource,并且输出控制器已经使用Always Copy和Do Not Copy进行了测试。 Plist已修订为 UIAppFonts Quicksand-Regular.tff

字体的可用名称只是Quicksand,但我尝试在应用程序中同时使用Quicksand和Quicksand Regular引用它。 我想补充一点, Style TargetType=“标签” Setter Property=“FontFamily”Value=“Quicksand” 风格

指向应用程序范围的资源字典,以及直接引用标签上的字体,都只是指定FontFamily并将其引用为平台特定的。 我看到的奇怪的事情是,在预览窗口中,它工作正常,标签和按钮文本都被正确替换,但在运行时无法正常工作


我相信资源实际上没有被正确地找到或复制到iOS项目中,但任何建议都将不胜感激。

我只能列出我为使字体在iOS上工作所做的工作:

字体为MaterialIcons-regular.ttf

已添加到资源文件夹

构建操作=BundleResources

复制到输出目录=不复制

Label FontFamily=“材质图标”(注意空格)

将字体添加到info.plist

<key>UIAppFonts</key>
<array>
    <string>MaterialIcons-Regular.ttf</string>
</array>
UIAppFonts

列出iOS上所需的步骤。它说复制到输出目录应该总是复制的。不是我发现了什么,而是另一个可能的绊脚石。

我只能列出我在iOS上使用字体所做的工作:

字体为MaterialIcons-regular.ttf

已添加到资源文件夹

构建操作=BundleResources

复制到输出目录=不复制

Label FontFamily=“材质图标”(注意空格)

将字体添加到info.plist

<key>UIAppFonts</key>
<array>
    <string>MaterialIcons-Regular.ttf</string>
</array>
UIAppFonts

列出iOS上所需的步骤。它说复制到输出目录应该总是复制的。不是我发现的,而是另一个可能的绊脚石。

要使用自定义字体,您需要执行以下操作:

**共享代码**

创建从元素派生的新类,要使用自定义字体显示,例如标签:

CustomFontLabel.cs

是的,它基本上是一个空类

Android 将字体(ttf文件)添加到应用程序的资产(不是资源!),并将构建操作设置为“AndroidAsset”

现在在android项目中创建自定义渲染器:

CustomFontRenderer.cs

[程序集:ExportRenderer(typeof(CustomFontLabel)、typeof(CustomFontRenderer))]
命名空间MyNamespace.Droid.Renderer.Elements
{
公共类CustomFontRenderer:LabelRenderer
{
公共CustomFontRenderer(上下文):基础(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
TextView标签=(TextView)控件;
if(e.NewElement?.FontFamily!=null)
{
字体字体=空;
//try-catch块将确保元素至少以默认值呈现
//在Xamarin预览器中使用系统字体,而不是使视图崩溃
尝试
{
font=Typeface.CreateFromAsset(AndroidApp.Application.Context.Assets,e.NewElement.FontFamily);
}
捕获(例外)
{
字体=字体。默认值;
}
标签。字体=字体;
}
}
}
iOS

将字体添加到resources文件夹,并确保构建操作设置为“BundleResource”

接下来,将字体添加到info.plist,例如:

  <key>UIAppFonts</key>
  <array>
    <string>fontawesome.ttf</string>
    <string>OpenSans-Light.ttf</string>
    <string>OpenSans-Bold.ttf</string>
    <string>OpenSans-LightItalic.ttf</string>
    <string>OpenSans-Italic.ttf</string>
    <string>OpenSans-Regular.ttf</string>
    <string>Lato-Bold.ttf</string>
    <string>Lato-Regular.ttf</string>
  </array>
UIAppFonts
font.ttf
OpenSans-Light.ttf
OpenSans-Bold.ttf
OpenSans-LightItalic.ttf
OpenSans-Italic.ttf
OpenSans-Regular.ttf
Lato-Bold.ttf
Lato-Regular.ttf
现在添加自定义渲染器:

CustomFontRenderer.cs

[程序集:ExportRenderer(typeof(CustomFontLabel)、typeof(CustomFontRenderer))]
命名空间MyNamespace.iOS.Renderer.Elements
{
公共类CustomFontRenderer:LabelRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.NewElement!=null&&e.NewElement.fontfamine!=null)
{
e、 NewElement.FontFamily=e.NewElement.FontFamily.Replace(“.ttf”,”);
}
}
}
}
现在回到表单视图,您可以插入自定义标签:

<elements:CustomFontLabel Text="A simple label using font family 'Lato'" FontFamily="Lato-Bold.ttf" />

要使用自定义字体,您需要执行以下操作:

**共享代码**

创建从元素派生的新类,要使用自定义字体显示,例如标签:

CustomFontLabel.cs

是的,它基本上是一个空类

Android 将字体(ttf文件)添加到应用程序的资产(不是资源!),并将构建操作设置为“AndroidAsset”

现在在android项目中创建自定义渲染器:

CustomFontRenderer.cs

[程序集:ExportRenderer(typeof(CustomFontLabel)、typeof(CustomFontRenderer))]
命名空间MyNamespace.Droid.Renderer.Elements
{
公共类CustomFontRenderer:LabelRenderer
{
公共CustomFontRenderer(上下文):基础(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
TextView标签=(TextView)控件;
if(e.NewElement?.FontFamily!=null)
{
字体字体=空;
//try-catch块将确保元素至少以默认值呈现
[assembly: ExportRenderer(typeof(CustomFontLabel), typeof(CustomFontRenderer))]
namespace MyNamespace.iOS.Renderer.Elements
{
    public class CustomFontRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null && e.NewElement.FontFamily != null)
            {
                e.NewElement.FontFamily = e.NewElement.FontFamily.Replace(".ttf", "");
            }
        }
    }
}
<elements:CustomFontLabel Text="A simple label using font family 'Lato'" FontFamily="Lato-Bold.ttf" />