Listview 在Xamarin表单中更改我的应用程序的字体(所有标签/列表视图…)

Listview 在Xamarin表单中更改我的应用程序的字体(所有标签/列表视图…),listview,button,fonts,label,xamarin.forms,Listview,Button,Fonts,Label,Xamarin.forms,我想用我的应用程序的自定义字体更改我的字体 在Xamarin文档中,我可以看到如何仅为一个标签而不是整个应用程序更改FontFamily 我使用适用于Android和IOS的Xamarin表单 我需要更改以下内容的字体: -列表视图 -钮扣 -标签 谢谢您可以使用应用程序将更改应用于应用程序中的所有元素。要更改所有标签元素的字体,可以执行以下操作: <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="htt

我想用我的应用程序的自定义字体更改我的字体

在Xamarin文档中,我可以看到如何仅为一个标签而不是整个应用程序更改FontFamily

我使用适用于Android和IOS的Xamarin表单

我需要更改以下内容的字体: -列表视图 -钮扣 -标签

谢谢

您可以使用应用程序将更改应用于应用程序中的所有元素。要更改所有标签元素的字体,可以执行以下操作:

<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.App">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Label">
                <Setter Property="FontFamily" Value="sans-serif" />
                <Setter Property="FontSize" Value="12" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

您可以使用应用程序将更改应用于应用程序中的所有元素。要更改所有标签元素的字体,可以执行以下操作:

<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Styles.App">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="Label">
                <Setter Property="FontFamily" Value="sans-serif" />
                <Setter Property="FontSize" Value="12" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

通过这种方式,您可以像预期的那样使用XAML代码:

<Label FontFamily="Arial" Text="Hi there" />

Android您可以覆盖所有需要的视图的默认渲染器。下面是标签的一个示例。您可以对listview和button执行相同的操作

[assembly: ExportRenderer (typeof (Label), typeof (MyLabelRenderer))]
namespace MyApp.Droid
{
    public class MyLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if ( !String.IsNullOrEmpty(Element.FontFamily) )
                Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, "Fonts/" + Element.FontFamily + ".otf");
        }
    }
}
[程序集:ExportRenderer(typeof(标签)、typeof(MyLabelRenderer))]
名称空间MyApp.Droid
{
公共类MyLabelRenderer:LabelRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
如果(!String.IsNullOrEmpty(Element.FontFamily))
Control.Typeface=Typeface.CreateFromAsset(Forms.Context.Assets,“Fonts/”+Element.FontFamily+“.otf”);
}
}
}
当然,您需要将其映射到嵌入自定义字体的位置。在我的例子中,资产/font/Arial.otf(构建操作:AndroidAsset)


iOS中,您需要将其添加到资源文件夹:Resources/Arial.otf(构建操作:BundleResource),并且它应该与上面的XAML代码一起工作。

这样,您可以像预期的那样使用XAML代码:

<Label FontFamily="Arial" Text="Hi there" />

Android您可以覆盖所有需要的视图的默认渲染器。下面是标签的一个示例。您可以对listview和button执行相同的操作

[assembly: ExportRenderer (typeof (Label), typeof (MyLabelRenderer))]
namespace MyApp.Droid
{
    public class MyLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if ( !String.IsNullOrEmpty(Element.FontFamily) )
                Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, "Fonts/" + Element.FontFamily + ".otf");
        }
    }
}
[程序集:ExportRenderer(typeof(标签)、typeof(MyLabelRenderer))]
名称空间MyApp.Droid
{
公共类MyLabelRenderer:LabelRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
如果(!String.IsNullOrEmpty(Element.FontFamily))
Control.Typeface=Typeface.CreateFromAsset(Forms.Context.Assets,“Fonts/”+Element.FontFamily+“.otf”);
}
}
}
当然,您需要将其映射到嵌入自定义字体的位置。在我的例子中,资产/font/Arial.otf(构建操作:AndroidAsset)


iOS中,您需要将其添加到资源文件夹:Resources/Arial.otf(构建操作:BundlerSource),它应该与上面的XAML代码一起工作。

您有字体示例吗?感谢您的帮助。如果我想使用自定义字体?就像这样:我看到了这篇文章,但它只针对一个标签,而不是我所有的标签app@user4071320-是的,但你要将那篇文章中的内容应用到我们评论的答案中的样式代码中。然后in应用于所有标签。例如,我有一个资源字典条目
。[加上适当位置的字体资源]然后在任何按钮/标签/条目
Style
,我知道
您有字体示例吗?谢谢您的帮助。如果我想使用自定义字体?就像这样:我看到了这篇文章,但它只针对一个标签,而不是我所有的标签app@user4071320-是的,但你要将那篇文章中的内容应用到我们评论的答案中的样式代码中。然后in应用于所有标签。例如,我有一个资源字典条目
。[加上适当位置的字体资源]然后在任何按钮/标签/条目
Style
,我做
你不需要为Android做自定义渲染器。有关如何使用
OnPlatform
正确定义字体,请参阅我对Jason答案的评论(展开以显示更多评论)。您不需要为Android定制渲染器。有关如何使用
OnPlatform
正确定义字体,请参阅我对Jason答案的评论(展开以显示更多评论)。