繁体   English   中英

Xamarin.Forms iOS应用中的Toolbaritem字体颜色

[英]Toolbaritem font color in Xamarin.Forms iOS app

我正在使用Xamarin.Forms编写iOS应用,我的工具栏字体颜色有问题。 我不能改变

在此处输入图片说明

<ContentPage.ToolbarItem>
<ToolbarItem Text="Menu2" Order="Secondary" />
<ToolbarItem Text="Menu1" Order="Secondary" />

如何将工具栏项目的字体颜色更改为红色? 我尝试过使用Navigationpage自定义渲染器,但是没有运气。

使用此链接中的代码

using System;
using Xamarin.Forms;
using ToolbarCustomFont.iOS;
using Xamarin.Forms.Platform.iOS;
using UIKit;

    [assembly: ExportRenderer(typeof (NavigationPage), typeof (CustomFontNavigationPageRenderer))]
    namespace ToolbarCustomFont.iOS
    {
        // https://blog.xamarin.com/custom-fonts-in-ios/
        public class CustomFontNavigationPageRenderer : NavigationRenderer
        {
            public CustomFontNavigationPageRenderer()
            {
            }

            const string customFontName = "FontAwesome.ttf";
            nfloat customFontSize = 10;

            public override void ViewWillAppear(bool animated)
            {
                base.ViewWillAppear(animated);

                if (this.NavigationBar == null) return;

                SetNavBarStyle();
    //          SetNavBarTitle();
                SetNavBarItems();
            }

            private void SetNavBarStyle()
            {
                NavigationBar.ShadowImage = new UIImage();
                NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
                UINavigationBar.Appearance.ShadowImage = new UIImage();
                UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
            }

            private void SetNavBarItems()
            {
                var navPage = this.Element as NavigationPage;

                if (navPage == null) return;

                var textAttributes = new UITextAttributes()
                {
                    Font = UIFont.FromName(customFontName, customFontSize)
                };

                var textAttributesHighlighted = new UITextAttributes()
                {
                    TextColor = Color.Black.ToUIColor(),
                    Font = UIFont.FromName(customFontName, customFontSize)
                };

                UIBarButtonItem.Appearance.SetTitleTextAttributes(textAttributes,
                    UIControlState.Normal);
                UIBarButtonItem.Appearance.SetTitleTextAttributes(textAttributesHighlighted,
                    UIControlState.Highlighted);
            }

        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM