Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize UINavigationBar font

Tags:

ios

ios5

I'm trying to customize my UINavigationBar font, using the following code for iOS 5 in my app delegate's application:didFinishLaunchingWithOptions:

if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) 
{
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                                                          [UIColor whiteColor], UITextAttributeTextColor,
                                                          [UIColor blackColor], UITextAttributeTextShadowColor,
                                                          [NSValue valueWithUIOffset:UIOffsetMake(1, 0)], UITextAttributeTextShadowOffset,
                                                          [UIFont fontWithName:kDefaultFont size:0.0], UITextAttributeFont, 
                                                          nil]];
}

It works fine and the navigation bar is rendered using my font. Great.

The references I've found suggest that you can use the font size of zero and it will resize the font to fit your navigation bar (using a slightly smaller font for the shorter navigation bar for the landscape layout). And it does choose a font size that fits nicely to the height of the navigation bar. But it looks like if you go from portrait to landscape and back, the width of the navigation bar's title label gets screwed up, so what shows up as a title of, for example, "Long Titlebar", looks fine when you first view it in portrait orientation, looks fine when you view it in landscape (with the appropriately smaller font), but when I come back to portrait, the font correctly reverts to the larger font, but the title text itself is truncated, becoming "Long..." even though there's plenty of space for the full title. Has anyone else seen this behavior when using a font size of 0.0?

Clearly, I can just specify an actual font size (in which case I don't see this truncating behavior), but then I'm manually figuring out what size to use. And worse, the font size is the same for landscape and portrait, so right now I'm using a font size that fits in the shorter landscape navigation bar title and the title is smaller than it needs to be in the taller portrait navigation bar.

Has anyone out there had experience with using setTitleTextAttributes to change the font of the [UINavigationBar appearance] in such a way that the font size changes between portrait and landscape, but not having this truncation of the title when you return back to portrait after going to landscape? I'm about to pursue various kludgy workarounds, but if you have any experience in this issue, let me know.

Update:

In the process of submitting this bug to Apple, I decided to demonstrate how to reproduce the problem:

  1. Create new iOS Master-Detail Application in Xcode 4.3.2.

  2. Put the above setTitleTextAttributes code in the app delegate's application:didFinishLaunchingWithOptions (I used the font @"GillSans").

  3. Go to MasterViewController and add line that says self.title = @"Long Title";

  4. Comment out the UIBarButtonItem *addButton code.

  5. Run the program. Note the title correctly says "Long Title". Rotate to landscape. Still looks good. Rotate back to portrait and the title now says "Long..." even though there's plenty of space.

  6. Curiously, if you restore the UIBarButtonItem *addButton code, the title works as it should. But if you either eliminate the UIBarButton item, or replace it with a button that uses initWithTitle rather than initWithBarButtonSystemItem, you get the problem with the navigation bar title after rotating from portrait to landscape and then back to portrait.

like image 811
Rob Avatar asked Apr 11 '12 16:04

Rob


2 Answers

By the way, I neglected to point out that Apple replied to my bug report, acknowledging that this was a known issue. Hopefully it will be resolved soon!

like image 196
Rob Avatar answered Oct 20 '22 22:10

Rob


the following is my workaround for this problem, and here's the comment that appears next to my workaround implementation in each case as a reminder to myself as to why i implemented this bit of code:

// when using an appearance proxy to set a custom font for the navigation bar (generally in
// application:didFinishLaunchingWithOptions: in the appDelegate code) for both iOS 5 & 6,
// there's a glitch that incorrectly auto-truncates the title in the following cirumstances:
//
// 1) when a 0.0 value is used for UITextAttributeFont in the titleTextAttributes dictionary
//    and a device/simulator running pre-iOS 5 rotates back to portrait from landscape
//    solution: perform [self.navigationController.navigationBar setNeedsLayout] in
//              didRotateFromInterfaceOrientation: the view controller in which the
//              auto-truncation is incorrectly occurring for systemVersion < 6.0
//
// 2) when a view initially loads running iOS 6 for a non-0.0 value for the UITextAttributeFont
//    in the titleTextAttributes dictionary
//    solution: perform [self.navigationController.navigationBar setNeedsLayout]
//              in viewDidLoad in the view controller in which the auto truncation is
//              incorrectly occurring for systemVersion >= 6.0

so, for the cases where i'm ok with using the 0.0 value for UITextAttributeFont but have to continue supporting iOS5, i use solution (1):

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
        && UIDevice.currentDevice.systemVersion.floatValue < 6.0)
        [self.navigationController.navigationBar setNeedsLayout];
}
#endif

and in the couple of cases of legacy code where i wanted to support iOS 6 and fix the glitch when the view first appears without having to re-write the MyAppAppearance class methods i have that set non-0.0 values for my [UINavigationBar appearance] titleTextAttributes, i found it easier to implement the solution (2) thus:

- (void)viewDidLoad
{
    [super viewDidLoad];
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
    if (UIDevice.currentDevice.systemVersion.floatValue >= 6.0)
#endif
        [self.navigationController.navigationBar setNeedsLayout];

    // … other viewDidLoadCode

(and the __IPHONE_OS_VERSION_MIN_REQUIRED bracket just helps remind me which code can eventually go away if desired in the future, and which may have to stay.)

to see a little more exactly what happens here, particularly in the case of the rotation, run the simulator with slow animations toggled.

like image 1
john.k.doe Avatar answered Oct 20 '22 21:10

john.k.doe