Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way of finding the font & frame size of the UINavigationBar title?

Is there any way of programatically returning the font and size of the navigation controller's title bar (e.g. self.title.frame.size or self.title.font, although I know neither of those works)?

(In case you're wondering the reason, I would like to use an NSString sizeWithFont method to work out how much text can fit in the title before I truncate it. I could do this by physically measuring the font and frame size myself (e.g. I think it's Helvetica 15), but I want the actual title to report its own font and size, to make my app future-proof.)

like image 475
Ric Levy Avatar asked May 17 '11 11:05

Ric Levy


People also ask

Is there a way to identify a font?

If the font you want to identify is in printed material like a magazine, you can find the name with a scanned image. Once you have a digital image, you can upload the image to a website like WhatTheFont. WhatTheFont 'reads' the font in your image and compares it to thousands it holds in its database.

Can I identify a font from an image?

Just upload an image, click the font you want to identify, then check out the results. For best results, upload a good quality image, and make sure the text is horizontal. We'll detect the text in the image automatically, then you can click the font you want.

Is there a Shazam for fonts?

The process is simple and fast. Once you spot a sexy typeface and take a photo of it with the app's built-in camera, WhatTheFont will isolate all the included text. You then select the one of interest, and the app will present you with a list of typefaces (and fonts) that it thinks could be a match.


2 Answers

As of iOS 5.0, UINavigationBar has a property called titleTextAttributes. This is a dictionary containing attributes described by the NSString UIKit Additions Reference.

If I print out this dictionary in one of my projects, I get the following:

Font = "<UICFFont: 0x998e690> font-family: \".Helvetica NeueUI\"; font-weight: bold; font-style: normal; font-size: 20px";
TextColor = "UIDeviceWhiteColorSpace 0.2 1";
TextShadowColor = "UIDeviceWhiteColorSpace 0 0";
TextShadowOffset = "UIOffset: {0, 0}";

...which shows the bold default system font at 20px. You can also change these as you see fit.

like image 92
MikeG Avatar answered Nov 14 '22 21:11

MikeG


I've worked out how to get the frame:

self.navigationItem.titleView.frame.size

Which just leaves the font. My research indicates that it's the System Font at size 20, but I'd love it to be self-reporting.

EDIT - well that's odd. If I do ...

 self.title = @"test";
 NSLog(@"Width is: %f", self.navigationItem.titleView.frame.size.width);

... I get random negative numbers from the NSLog. So it doesn't look like the title frame is right.

like image 39
Ric Levy Avatar answered Nov 14 '22 21:11

Ric Levy