Xamarin形式:UWP和Windows 8.1中的自定义字体 [英] Xamarin forms: Custom Fonts in UWP and Windows 8.1
问题描述
我正在开发一个使用font.otf文件的应用程序.我的应用程序将在android,ios,Windows 10和Windows 8.1上运行.我需要为标签创建样式以设置字体系列. 对于android和ios,我引用了链接
I am developing an app in which I am using a font.otf file. My app will run on android, ios, windows 10 and windows 8.1. I need to create styles for my labels to set font family. For android and ios I referenced this link
我在这样的xaml页面中尝试过-
I tried in xaml page like this-
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS></OnPlatform.iOS>
<OnPlatform.Android>Lobster-Regular.ttf#Lobster-Regular</OnPlatform.Android>
<OnPlatform.WinPhone></OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
new Label {
Text = "Hello, Forms!",
FontFamily = Device.OnPlatform (
null,
null,
@"\Assets\Fonts\Lobster-Regular.ttf#Lobster-Regular"
// Windows Phone will use this custom font
)
}
但是,当我运行我的应用程序时,Windows 10和8.1的字体没有设置.
But when I run my app Font does not set for Windows 10 and 8.1.
如何为Windows 10和8.1设置字体系列. 还是有一种更有效的方式来覆盖所有平台来应用字体系列?
How can I set font family for windows 10 and 8.1. Or is there more efficient way to apply font family with covering all the platforms?
推荐答案
Note: there is a bug where custom fonts do not work if you are using a NavigationPage
在Windows 8.1/UWP上,自定义字体不需要自定义渲染器; Xamarin示例代码仅存在几个错误:
You do not need a custom renderer for custom fonts on Windows 8.1/UWP; the Xamarin sample code simply has a couple of mistakes:
- 使用正斜杠('/')代替
- 路径应为
[font file]#[font name]
的形式(无字体样式,例如'regular')
- Use a forward slash ('/') instead
- The path should be in the form of
[font file]#[font name]
(without the font style, e.g. 'regular')
所以这种情况下的路径实际上应该是
So the path in this case should actually be
"Assets/Fonts/Lobster-Regular.ttf#Lobster"
您还可以在Xaml中使用它
And you can also use this in Xaml
<OnPlatform.WinPhone>Assets/Fonts/Lobster-Regular.ttf#Lobster</OnPlatform.WinPhone>
确保字体文件包含在BuildAction:Content
项目中,并且可以正常工作.
Make sure your font file is included in your project with BuildAction:Content
and it should work.
这篇关于Xamarin形式:UWP和Windows 8.1中的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!