簡體   English   中英

更改應用程序的默認字體 Xamarin Forms

[英]Change default font of the application Xamarin Forms

我需要更改應用程序的默認字體,以更改 Tabs 和 NavigationBar 的字體。 對不起,我的英語不好。 在此處輸入圖像描述

要更改 NavigationBar 中的標題字體,請閱讀此文檔,在每個 contentPage 中自定義Shell.TitleView

<Shell.TitleView>
    <Label Text="customTitle" FontSize="30"/>
</Shell.TitleView>

要更改標簽欄標題字體,您需要一個自定義渲染器:

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

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

    public class CustomBottomNavAppearance : IShellBottomNavViewAppearanceTracker
    {
        public void Dispose()
        {

        }

        public void ResetAppearance(BottomNavigationView bottomView)
        {

        }

        public void SetAppearance(BottomNavigationView bottomView, ShellAppearance appearance)
        {

            IMenu menu = bottomView.Menu;
            for (int i = 0; i < bottomView.Menu.Size(); i++)
            {
                IMenuItem menuItem = menu.GetItem(i);
                var title = menuItem.TitleFormatted;
                SpannableStringBuilder sb = new SpannableStringBuilder(title);

                int a = sb.Length();
                
                //here I set fontsize 20
                sb.SetSpan(new AbsoluteSizeSpan(20,true), 0, a, SpanTypes.ExclusiveExclusive);

                menuItem.SetTitle(sb);
            }

        }
    }
}

結果如下:

在此處輸入圖像描述

此處已上傳示例項目,您可以查看它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM