【问题标题】:FontFamily on Shell -> TabBar -> Tab -> TitleShell 上的 FontFamily -> TabBar -> Tab -> Title
【发布时间】:2019-09-22 05:38:19
【问题描述】:

有没有办法设置这个字体系列?我无法找到如何做到这一点。我可以通过什么方式实现它?

【问题讨论】:

  • 可以吗?

标签: xamarin.forms


【解决方案1】:

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

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

[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 中添加:

 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 被放入Resources

【讨论】:

  • 非常感谢。我必须设置代码,将 ResetAppearance 方法的代码复制到 SetAppearance 并且它可以工作。它清除了我在 XAML 中设置的字体颜色和背景颜色。我想我需要在渲染器上设置它。我是对的?
  • @GeorgeMR 是的,你可以使用sb.SetSpan(new ForegroundColorSpan(Android.Graphics.Color.Red), 0, sb.Length(), SpanTypes.InclusiveInclusive); 设置颜色
  • 谢谢利奥。你太棒了。
  • @GeorgeMR 如果回答对你有帮助,可以标记一下吗,谢谢
  • @GeorgeMR 您可以尝试使用自定义渲染器的底部导航
【解决方案2】:

Leo Zhu 的回答是正确的,但直到我将 ResetAppearance(bottomView) 放入 SetAppearance 方法后才对我有用。我把这个:if (title == null) { continue; } 放在ResetAppearance 方法中。

首先,在您的Android文件夹中,您需要将internal static MainActivity Instance { get; private set; }添加到MainActivity.cs中的MainActivity类,并在同一类中的OnCreate方法中通过Instance = this;对其进行初始化。

public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
    ResetAppearance(bottomView);
}


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;
       if (title == null) { continue; } 
       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);           
   }
}

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2021-03-19
    • 2020-04-05
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多