Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Xamarin Forms - iOS TabbedPage Tab Font Overrides
using System;
using {AppName}.iOS.Renderers;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using {AppName}.iOS.Views;
[assembly: ExportRenderer(typeof(TabbedPage), typeof(ExtendedTabbedPageRenderer))]
namespace {AppName}.iOS.Renderers
{
/// <summary>
/// This subclass of <see cref="TabbedRenderer"/> changes the appearance of the pages attached to a <see cref="TabbedPage"/> instance.
/// </summary>
public class ExtendedTabbedPageRenderer : TabbedRenderer
{
/// <summary>
/// This method is overridden to allow the tab bar items to have their fonts updated appropriately.
/// </summary>
/// <param name="animated">Whether it's animated or not.</param>
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
UpdateAllTabBarItems();
}
/// <summary>
/// Updates the tab bar item for each page included in the TabbedPage.
/// </summary>
private void UpdateAllTabBarItems()
{
foreach(var controller in ViewControllers)
{
controller.TabBarItem.SetTitleTextAttributes(StandardAttributes, UIControlState.Normal);
}
}
/// <summary>
/// Stores the UITextAttributes for this class.
/// </summary>
/// <value>The standard attributes.</value>
private static UITextAttributes StandardAttributes { get; } = new UITextAttributes { Font = FontManager.GetFont(9.0f)};
}
}
using System;
using UIKit;
using {AppName}.View;
namespace {AppName}.iOS.Views
{
/// <summary>
/// The FontManager type is responsible for setting font changes where Xamarin.Forms elements do not have accessible font family properties.
/// </summary>
public static class FontManager
{
/// <summary>
/// This method should be executed during program launch to configure the rendering.
/// </summary>
public static void ConfigureFonts()
{
var titleAttribute = new UITextAttributes { Font = GetFont(UIFont.LabelFontSize) };
// Updates the ToolbarItem appearance.
UIBarButtonItem.Appearance.SetTitleTextAttributes(titleAttribute, UIControlState.Normal);
// Sets the NavigationBar title items. Also sets the format for TabbedPage tab items for some reason.
UINavigationBar.Appearance.SetTitleTextAttributes(titleAttribute);
// Just being thorough...
UILabel.Appearance.Font = GetFont(UIFont.LabelFontSize);
}
/// <summary>
/// Gets a <see cref="UIFont"/> object for the standard font with the desired size.
/// </summary>
/// <returns>UIFont object for use.</returns>
/// <param name="fontSize">Font size.</param>
public static UIFont GetFont(nfloat fontSize)
{
return UIFont.FromName(StandardFontName, fontSize);
}
/// <summary>
/// Gets the iOS font name for the standard font for this application.
/// </summary>
/// <value>The name of the standard font.</value>
public static string StandardFontName => "AvenirNextCondensed-Regular";
}
}
@VyasPrachi

This comment has been minimized.

Copy link

@VyasPrachi VyasPrachi commented Dec 5, 2018

Hello richseviora ,
I have applied this code in my xamarin froms application for IOS but size of tabbedpage title is not change. Only fontfamily change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment