Shell 上的 FontFamily ->标签栏 ->标签 ->标题 [英] FontFamily on Shell -> TabBar -> Tab -> Title

查看:27
本文介绍了Shell 上的 FontFamily ->标签栏 ->标签 ->标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将其设置为字体系列?我无法找到如何去做.我可以通过什么方式实现这一点?

Is there a way to set this a font family? I'm unable to find how to do it. In what way I can implement this?

推荐答案

由于我没有找到要设置的直接属性,这里我使用 Custom Renderen 来实现它(适用于 Android):

as i didn't find the direct property to set,here i use Custom Renderen to achive it (for Android):

在 Droid 项目中创建一个 AndroidShell.cs:

create a AndroidShell.cs in Droid project:

[assembly: ExportRenderer(typeof(ShellTest.AppShell), typeof(AndroidShell))]
namespace ShellTest.Droid
{
  class AndroidShell : ShellRenderer
   { 
      public AndroidShell(Context context) : base(context)
       {
       }

      protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
      {
        return new MyBottomNavigationView(this);
      }
   }
}

创建MyBottomNavigationView.cs:

internal class MyBottomNavigationView: IShellBottomNavViewAppearanceTracker
{
    private AndroidShell androidShell;

    public MyBottomNavigationView(AndroidShell androidShell)
    {
        this.androidShell = androidShell;
    }

    public void Dispose()
    {

    }

    public void ResetAppearance(BottomNavigationView bottomView)
    {

        IMenu menu = bottomView.Menu;
        for (int i = 0; i < bottomView.Menu.Size(); i++)
        {
            IMenuItem menuItem = menu.GetItem(i);
            var title = menuItem.TitleFormatted;
            Typeface typeface = Typeface.CreateFromAsset(MainActivity.Instance.Assets, "HYXuJingXingKaiW.ttf");
            SpannableStringBuilder sb = new SpannableStringBuilder(title);

            sb.SetSpan(new CustomTypefaceSpan("",typeface), 0, sb.Length(), SpanTypes.InclusiveInclusive);
            menuItem.SetTitle(sb);           
        }
    }

    public void SetAppearance(BottomNavigationView bottomView, ShellAppearance appearance)
    {
    }
}

创建CustomTypefaceSpan.cs:

class CustomTypefaceSpan :TypefaceSpan
{
    private  Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) : base(family)
    {
        newType = type;
    }
    public override void UpdateDrawState(TextPaint ds)
    {
        applyCustomTypeFace(ds,newType);

    }
    public override void UpdateMeasureState(TextPaint paint)
    {
        applyCustomTypeFace(paint,newType);
    }
    private static void applyCustomTypeFace(Paint paint, Typeface tf)
    {
        TypefaceStyle oldStyle;
        Typeface old = paint.Typeface;
        if (old == null)
        {
            oldStyle = 0;
        }
        else
        {
            oldStyle = old.Style;
        }

        TypefaceStyle fake = oldStyle & ~tf.Style;
        if ((fake & TypefaceStyle.Bold) != 0)
        {
            paint.FakeBoldText = true;
        }

        if ((fake & TypefaceStyle.Italic) != 0)
        {
            paint.TextSkewX = -0.25f;
        }

        paint.SetTypeface(tf);
    }
}

HYXuJingXingKaiW.ttf放在Resources/Assets中,BuildAction:AndroidAsset

ios 中,您可以尝试在 AppDelegate.cs 中添加:

in ios,you could try add this in AppDelegate.cs :

 UITabBarItem.Appearance.SetTitleTextAttributes(new UITextAttributes() {Font=UIFont.FromName("HYXuJingXingKaiW.ttf",14), },UIControlState.Normal);
 UITabBarItem.Appearance.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.FromName("HYXuJingXingKaiW.ttf", 14), }, UIControlState.Selected);

HYXuJingXingKaiW.ttf放在资源

HYXuJingXingKaiW.ttf is put inside Resources

这篇关于Shell 上的 FontFamily ->标签栏 ->标签 ->标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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