Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Problem with Custom Renderer

$
0
0

Following a guide online, I've created a custom renderer for a navigation page with the goal of changing the font and color of a toolbar caption.

To start, I set up a custom NavigationPage in the PCL as follows:

public class FontNavigationPage : NavigationPage
{
    public FontNavigationPage(Page root) : base(root)
    {
        BarBackgroundColor = Color.FromHex("#7DBBD9");
        BarTextColor = Color.Blue;
    }
}

Then the custom renderer for Droid:

[assembly: ExportRenderer(typeof(FontNavigationPage), typeof(FontNavigationPageRenderer))]

namespace XXXXXXXXX.Droid.Renderers
{
public class FontNavigationPageRenderer : NavigationPageRenderer
{
private Android.Support.V7.Widget.Toolbar toolbar;

    public override void OnViewAdded(Android.Views.View child)
    {
        base.OnViewAdded(child);
        if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
        {
            toolbar = (Android.Support.V7.Widget.Toolbar)child;
            toolbar.ChildViewAdded += Toolbar_ChildViewAdded;
        }
    }

    void Toolbar_ChildViewAdded(object sender, ChildViewAddedEventArgs e)
    {
        var view = e.Child.GetType();

        if (e.Child.GetType() == typeof(Android.Widget.TextView))
        {
            var textView = (Android.Widget.TextView)e.Child;
            var spaceFont = Typeface.CreateFromAsset(Xamarin.Forms.Forms.Context.ApplicationContext.Assets, "Lusitana-Regular.ttf");
            textView.Typeface = spaceFont;
            toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
        }
        //If your app is not using the AppCompatTextView, you don't need this check
        if (e.Child.GetType() == typeof(Android.Support.V7.Widget.AppCompatTextView))
        {
            var textView = (Android.Support.V7.Widget.AppCompatTextView)e.Child;
            var spaceFont = Typeface.CreateFromAsset(Xamarin.Forms.Forms.Context.ApplicationContext.Assets, "Lusitana-Regular.ttf");
            textView.Typeface = spaceFont;
            toolbar.ChildViewAdded -= Toolbar_ChildViewAdded;
        }
    }
}

}

This works in that the toolbar caption appears in the desired custom font.

However, I cannot change the color of the type. I thought that changing BarTextColor = Color.Blue; would do the job, but it has no effect. The text remains dark blue.

Can anyone suggest how to change the textcolor?

Thanks


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>