XAMARIN - 是否可以在我的应用程序中使用 default/system 字体?

XAMARIN - is it possible to use default/system fonts in my app?

我浏览了 3 台 Android 设备(SGS6、SGS20、H10)上的“/system/fonts”文件夹,将所有文件从那里下载到我的 PC,分析它并发现一些字体名称所有 3 种设备都通用 - AndroidClock、Carrois Gothic SC、Coming Soon、Cutive Mono、Dancing Script、Droid Sans Mono、Noto Naskh、Noto Sans、Noto Serif、Roboto、Roboto Black、Roboto Condensed、Roboto Condensed轻型、Roboto 轻型、Roboto 中型、Roboto 薄型。

问题 - 是否可以在我的 Xamarin 应用程序中使用其中任何一个而无需明确绑定它们? 如果我只是指定 myLabel.FontFamily = "Coming Soon" 并且可行……但实际上根本行不通,那就太好了。 :-( 所以,我有点不解——为什么?如果可能的话,让它以某种方式工作?...

我尝试在我的 Xamarin 应用程序中显示 10 个标签,所有标签都具有不同的 FontFamily 名称:AndroidClock、Carrois Gothic SC、即将推出、Cutive Mono、Dancing Script、Droid Sans、Droid Serif、Droid Sans Mono - 但所有标签都以相同的字体显示(我什至不确定默认字体的名称是什么)。

我需要它只适用于 Android。对 iOS 一点都不感兴趣。 有没有办法告诉 Xamarin 仅使用设备上可用的系统字体?

来自 Fonts in Xamarin Forms

Controls that display text can set the FontFamily property to a font family name, such as "Times Roman". However, this will only work if that font family is supported on the particular platform.

There are a number of techniques that can be used to attempt to derive the fonts that are available on a platform. However, the presence of a TTF (True Type Format) font file does not necessarily imply a font family, and TTFs are often included that are not intended for use in applications. In addition, the fonts installed on a platform can change with platform version. Therefore, the most reliable approach for specifying a font family is to use a custom font.

It would be nice if I just to specify myLabel.FontFamily = "Coming Soon" and that works... but in real that does not work at all. :-( So, I'm a bit puzzled - why? And if possible to make it working somehow?...

如果你解析 fonts.xml 文件,你可以找到哪个字体系列使用哪个字体(参见 here):

╔════╦════════════════════════════╦═════════════════════════════╗
║    ║ FONT FAMILY                ║ TTF FILE                    ║
╠════╬════════════════════════════╬═════════════════════════════╣
║  1 ║ casual                     ║ ComingSoon.ttf              ║
║  2 ║ cursive                    ║ DancingScript-Regular.ttf   ║
║  3 ║ monospace                  ║ DroidSansMono.ttf           ║
║  4 ║ sans-serif                 ║ Roboto-Regular.ttf          ║
║  5 ║ sans-serif-black           ║ Roboto-Black.ttf            ║
║  6 ║ sans-serif-condensed       ║ RobotoCondensed-Regular.ttf ║
║  7 ║ sans-serif-condensed-light ║ RobotoCondensed-Light.ttf   ║
║  8 ║ sans-serif-light           ║ Roboto-Light.ttf            ║
║  9 ║ sans-serif-medium          ║ Roboto-Medium.ttf           ║
║ 10 ║ sans-serif-smallcaps       ║ CarroisGothicSC-Regular.ttf ║
║ 11 ║ sans-serif-thin            ║ Roboto-Thin.ttf             ║
║ 12 ║ serif                      ║ NotoSerif-Regular.ttf       ║
║ 13 ║ serif-monospace            ║ CutiveMono.ttf              ║
╚════╩════════════════════════════╩═════════════════════════════╝

因此ComingSoon.ttf在FontFamily中被称为casual

<Label Text="Hello monospace!"
        FontFamily="monospace"
        HorizontalOptions="CenterAndExpand" />
<Label Text="ComingSoon"
        HorizontalOptions="CenterAndExpand"
        FontFamily="casual"/>
<Label Text="Roboto Black"
        HorizontalOptions="CenterAndExpand"
        FontFamily="sans-serif-black"/>
<Label Text="Default Font"
        HorizontalOptions="CenterAndExpand" />

效果:

这里是.