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

Change ActionBar font on customrenderer

$
0
0

I'm trying to change the font of navigationbar on my application using custom renderers. So far I was able to achieve this on IOS using it like

`public class NavigationRenderer : Xamarin.Forms.Platform.iOS.NavigationRenderer
{
public override void ViewDidLoad()
{
base.ViewDidLoad();

        this.NavigationBar.TitleTextAttributes=new UIStringAttributes()
        {
            Font = UIFont.FromName("Comfortaa",17f),
            ForegroundColor = UIColor.White

        };

    }`

However on android I was not able to achieve this at all. On IOS I used the NavigationRenderer since I did not need the title for the page however, with the way I found on android you needed to give it the title of the page because the spannablestring needed it. Like this:

`if (e.NewElement.Title != null) {
SpannableString s = new SpannableString(e.NewElement.Title);

            s.SetSpan(new TypefaceSpan("ComfortaaRegular.ttf"), 0, s.Length(),
                SpanTypes.ExclusiveExclusive);

            // Update the action bar title with the TypefaceSpan instance

            var actionBar = ((Activity) Context).ActionBar;

        }`

To sum up I did it like this:

`public class PageRenderer : Xamarin.Forms.Platform.Android.PageRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);

        if (e.NewElement.Title != null) {
            SpannableString s = new SpannableString(e.NewElement.Title);

            s.SetSpan(new TypefaceSpan("ComfortaaRegular.ttf"), 0, s.Length(),
                SpanTypes.ExclusiveExclusive);

            // Update the action bar title with the TypefaceSpan instance

            var actionBar = ((Activity) Context).ActionBar;
            actionBar.TitleFormatted = s;

        }
    }

}`

This seems to have no effect at all. Any help would be helpful.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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