如何在Xamarin ActionBar标题中设置自定义字体系列? [英] How to Set a Custom Font Family in the Xamarin ActionBar Title?

查看:91
本文介绍了如何在Xamarin ActionBar标题中设置自定义字体系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与标题相同.我在这里在Xamarin.Forms NavigationBar + ActionBar中自定义字体告诉您要在style.xml文件中进行定义,但是我无法获得自定义字体系列.

As in title. I found the idea how to do this here Custom Fonts in Xamarin.Forms NavigationBar + ActionBar that tells to define this in style.xml file but I'm not able to get the custom font family.

所以我的风格现在看起来像这样:

So my style look like this right now:

 <style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<!--set your custom font properties-->
<item name="android:textSize">18sp</item>
<item name="android:fontFamily">HelveticaNeue Medium.ttf</item>
<item name="android:textColor">@android:color/white</item>

推荐答案

您可以为此在android中创建自定义渲染器.渲染器仅使用android项目中定义的资源即可更改导航栏的字体.您可以在android中使用以下渲染器:

You can create a custom renderer in android for this purpose. The renderer simply changes the typeface of your navigation bar using the resource defined in your android project. You can use the following renderer in android:

[assembly: ExportRenderer(typeof(NavigationPage), typeof(AndroidNavigationRenderer))]
namespace Xamarin.Droid.DependencyServices
{
  public class AndroidNavigationRenderer : NavigationPageRenderer
  {
    private Android.Support.V7.Widget.Toolbar toolbar;

    public override void OnViewAdded(Android.Views.View child)
    {
        base.OnViewAdded(child);
        if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
        {
            toolbar = (Android.Support.V7.Widget.Toolbar)child;
            toolbar.ChildViewAdded += Toolbar_ChildViewAdded;
        }
    }

    private void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
    {
        var view = e.Child.GetType();
        if (e.Child.GetType() == typeof(Android.Support.V7.Widget.AppCompatTextView))
        {
            var textView = (Android.Support.V7.Widget.AppCompatTextView)e.Child;
            textView.Typeface = Typeface.CreateFromAsset(this.Context.Assets, "fonts/QuicksandRegular.ttf");

            toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
        }
    }
  }
}

对于UWP,您可以在app.xaml中定义自定义样式,以覆盖标题栏的默认样式.您可以在UWP中使用以下代码:

For UWP, you can define a custom style in your app.xaml to override the default style of your title bar. You can use the following code in UWP:

 <Style x:Key="TitleTextBlockStyle" TargetType="TextBlock">
    <Setter Property="FontSize" Value="18"/>
    <Setter Property="FontStyle" Value="Normal"/>
    <Setter Property="FontWeight" Value="SemiBold"/>
    <Setter Property="FontFamily" Value="Assets/Fonts/QuicksandRegular.ttf#Quicksand"/>
    <Setter Property="Padding" Value="10"/>
 </Style>

只需将字体系列更改为您选择的字体系列.

Simply change the font family to the font family of your choice.

这篇关于如何在Xamarin ActionBar标题中设置自定义字体系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆