FontFamily on Shell -> TabBar -> Tab -> Title

寵の児 提交于 2020-07-10 08:38:11

问题


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?


回答1:


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

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);
      }
   }
}

create 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)
    {
    }
}

create 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);
    }
}

and HYXuJingXingKaiW.ttf is put inside Resources/Assets with BuildAction:AndroidAsset

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 is put inside Resources



来源:https://stackoverflow.com/questions/58046350/fontfamily-on-shell-tabbar-tab-title

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!