xamarin - 我需要从 .ttf 文件设置字体

标签 xamarin xamarin.ios xamarin.android xamarin.forms

我正在尝试为我的系统设置字体并在我的应用程序中使用它它适用于 android 但不适用于 IOS - 首先我在IOS项目的资源中添加了.ttf文件

enter image description here

然后将此字体添加到 Entitlements.plist

enter image description here

对于 Android,我添加到 Assets 文件夹

enter image description here

然后在主应用程序中的 App 文件我在字典资源中添加了这段代码

 <Style x:Key="LableStyle" TargetType="Label" >
        <Setter Property="Label.FontFamily">
            <Setter.Value>
                <OnPlatform x:TypeArguments="x:String">
                    <OnPlatform.Android>
                        DroidSansArabic.ttf#DroidSansArabic
                            </OnPlatform.Android>
                    <OnPlatform.iOS>
                        DroidSansArabic
                    </OnPlatform.iOS>
                </OnPlatform>
            </Setter.Value>
        </Setter>
    </Style>

对于标签,我添加了这样的样式

<Label HorizontalOptions="StartAndExpand" 
   Margin="20,5,0,0"
   x:Name="lblForgotPassword"
   Text="Forgot Password"
   Style="{StaticResource LableStyle}">
</Label>

但它在 Android 上运行良好,但在 IOS 上却无法运行,为什么?

最佳答案

我假设您的字体系列名称在 iOS 上不正确。 macOS/iOS 字体名称不是没有扩展名的文件名,名称是在 ttf 本身中定义的。

我的 macOS 上的 DroidSansArabic.ttf 和我在 iOS 上使用的那个使用了 B Tabassom 的姓氏

所以它在表单中的用法是:

label.FontFamily = Device.OnPlatform(
    iOS: "B Tabassom",
    Android: "DroidSansArabic.ttf#DroidSansArabic",
    WinPhone: null
);

由于周围有许多 DroidSansArabic.ttf,请通过在 macOS 的字体簿中打开 ttf 并查看标题栏来验证您的字体系列名称:

enter image description here

如果您的 macOS 已经安装了它,请查看名称列:

enter image description here

更新:您还在错误的 .plist 中输入了 UIAppFonts 键,它进入了 Info.plist:

<key>UIAppFonts</key>
        <array>
                <string>DroidSansArabic.ttf</string>
        </array>
</dict>

关于xamarin - 我需要从 .ttf 文件设置字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45768488/

相关文章:

c# - 保存图像缩略图而不是 MediaStore.ActionImageCapture 中的完整图像

android - Xamarin、C#、Google map fragment 不响应任何命令。 (即移动相机)

c# - 错误代码 :XamlC error XFC0000 : Cannot resolve type "MediaElement"

android - 如何防止应用程序被缓存在Android中

android - 带有 Mvvmcross 的 Xamarin 表单选项卡式页面

c# - 如何用 Device.OnPlatform 翻译这个 Padding? (iOS 案例)

xamarin - Visual Studio 应用中心 : crashes and errors not working on Xamarin iOS

ios - Mono 框架或 Mono 运行时是 Xamarin IOS ipa 包的一部分吗?

xamarin - iPhone 启动图像中没有横向选项

.net - 如何使用.Net Core 1.0开发Android应用程序?