2016-09-19 2 views
0

Ich schreibe iOS App mit Xamarin.Forms und ich habe ein Problem mit meiner Symbolleiste Schriftfarbe. Ich kann es nicht ändern.Toolbaritem Schriftfarbe in Xamarin.Forms iOS App

enter image description here

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

Wie ändere ich die Elemente der Symbolleiste Schriftfarbe heißt Red? Ich habe versucht, mit Navigationspage benutzerdefinierten Renderer, aber ohne Glück.

Antwort

1

Benutzen Sie den Code aus diesem 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); 
      } 

     } 
    } 
Verwandte Themen