【问题标题】:Toolbaritem font color in Xamarin.Forms iOS appXamarin.Forms iOS 应用程序中的 Toolbaritem 字体颜色
【发布时间】:2016-09-19 17:27:43
【问题描述】:

我正在使用 Xamarin.Forms 编写 iOS 应用程序,但我的工具栏字体颜色存在问题。我无法改变它。

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

如何将工具栏项的字体颜色更改为红色?我曾尝试使用导航页面自定义渲染器,但没有成功。

【问题讨论】:

    标签: ios xamarin.forms


    【解决方案1】:

    使用来自link的代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多