默认字体系列是什么?

2024-01-26

这返回null:

DefaultFontFamily = Font.SystemFontOfSize(NamedSize.Default).FontFamily;

还有这个:

DefaultFontFamily = new Label().FontFamily;

正确的做法是什么?


没有正确的方法来做到这一点 - 因为这里没有跨平台的“默认字体系列”,而且它是特定于平台的。

为了进一步详细说明这一点,想参考源代码中定义的此属性Label https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/Label.cs#L30,

public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;

这又指的是定义在的依赖属性FontElement https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/FontElement.cs#L11,

public static readonly BindableProperty FontFamilyProperty =
        BindableProperty.Create("FontFamily", typeof(string), typeof(IFontElement), default(string),
                                propertyChanged: OnFontFamilyChanged);

或您提到的静态方法SystemFontOfSize https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/Font.cs#L67.

public static Font SystemFontOfSize(NamedSize size)
{
    var result = new Font { NamedSize = size };
    return result;
}

正如您所看到的,在这两种情况下,根据源代码,值'null' for FontFamily并不意外。 (在第一种情况下,依赖属性的默认值定义为default(string)这会转换为 null,在第二种情况下,值为FontFamily从未设置)

同样适用于Font.Default https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/Font.cs#L26。进一步挖掘,你会发现IsDefault https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/Font.cs#L16其定义如下:

public bool IsDefault
{
    get { return FontFamily == null && FontSize == 0 && NamedSize == NamedSize.Default && FontAttributes == FontAttributes.None; }
}

So if FontFamily is null,那么它就是默认的在xamarin中形成生态系统。


When Label确实在特定平台上渲染,除非它FontFamily属性是专门修改自'null'对于某个值,本机控件上的字体属性也永远不会被修改。因此,本机控件使用该平台的默认字体系列进行呈现。

为了获得默认字体,您必须实现本机服务,并使用依赖注入 https://visualstudiomagazine.com/articles/2015/09/01/native-services-with-xamarinforms.aspx以获得该值。

有关用于本机控件的更多详细信息Label- 你可以参考这个link https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/renderers/#Views.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

默认字体系列是什么? 的相关文章

随机推荐