Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 14    Views: 160

Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
01/27/14 02:19 PM (11 years ago)

Change font size of NavBar?

Looking for a simple way to globally change the font size in my nav bar. I looked through the BT_viewController.m file, but didn't see any font sizes listed to change. I've also searched around, and found instructions (from David) for completely adding new fonts... but that seems a bit overkill. Not sure what the default font size is, but if it's 16, I simply need to make it 14... I have a couple of letters of my home page nav bar title being cut off, where it cuts off the last 3 letters and replaces with "...". Simply dropping the default nav bar font size down a couple of points should do the trick.
 
shenry
Aspiring developer
Profile
Posts: 469
Reg: Jan 10, 2012
Orange County, ...
13,390
like
01/27/14 08:25 PM (11 years ago)
Here's a post that explains how to do it. http://www.appcoda.com/customize-navigation-status-bar-ios-7/
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
01/27/14 08:30 PM (11 years ago)
Nice link. Thanks! Cheers! -- Smug
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
01/28/14 05:06 AM (11 years ago)
Can't get it to work. I'm adding the following to my BT_viewController.m file (using size 10 so I can see a drastic difference if it works), around line 541 [[UINavigationBar appearance] setTitleTextAttributes: [UIFont fontWithName:@"Helvetica-Bold" size:10.0]]; Am I right in thinking that if it's a system font, I don't need to create a dictionary key for it in my plist? I used David's trick with the code snippet in my BT_Device.m file to list all available system fonts in the debug console, and the font referenced in the code above is in fact listed. When I run it in either the simulator or my device, the nav bar title font doesn't change :( Any ideas?
 
shenry
Aspiring developer
Profile
Posts: 469
Reg: Jan 10, 2012
Orange County, ...
13,390
like
01/28/14 05:25 AM (11 years ago)
Did you put it somewhere in the beginning of AppDelegate.m
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
01/28/14 05:33 AM (11 years ago)
Tried that too, but get a sigbart error, basically telling me that it can't find the font in the dictionary... but it's a system font. Here's the console error when adding the above code to appDelegate.m 2014-01-28 07:29:08.256 universal[21842:70b] -[UICTFont _ui_attributesForDictionaryContainingUIStringDrawingKeys]: unrecognized selector sent to instance 0xd78ad70 2014-01-28 07:29:08.260 universal[21842:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICTFont _ui_attributesForDictionaryContainingUIStringDrawingKeys]: unrecognized selector sent to instance 0xd78ad70' oh, how I miss the days of programming in MS Visual Basic.... font.size=10 lol
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
01/28/14 07:23 AM (11 years ago)
heh. I miss VB6, lol! I transcribed (not quite 'wrote') a little tutorial on adding fonts to an iOS project. Granted, some things might need an update with iOS 7, but on page 6 of the document is a bit of code that will 'write out' all of the fonts in your system. You may want to do that 'just to be sure' that the system font you're using actually *is* in the system. https://www.buzztouch.com/files/howtos/btcustomfonts.pdf Cheers! -- Smug
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
01/28/14 07:39 AM (11 years ago)
Hey Smug, Actually, I already did that. I followed David's code that he posted, and added to bt_Device.m. The code I used is shown below, and it printed the fonts just fine... and showed the "Helvetica-Bold" font family that I referenced in the code that I inserted, so I'm not sure why it's not working. ------------------------------------ Here's what I used to print the fonts available, which worked, and showed Helvetica-Bold as available ------------------------------------- //show all the custom font names available... [BT_debugger showIt:self message:[NSString stringWithFormat:@"Listing custom fonts (UIAppFonts) listed in app's .plist...%@", @""]]; NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary]; NSArray* fontFiles = [infoDict objectForKey:@"UIAppFonts"]; for (NSString *fontFile in fontFiles) { NSURL *url = [[NSBundle mainBundle] URLForResource:fontFile withExtension:NULL]; NSData *fontData = [NSData dataWithContentsOfURL:url]; CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData); CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider); NSString *fullName = CFBridgingRelease(CGFontCopyFullName(loadedFont)); CGFontRelease(loadedFont); CGDataProviderRelease(fontDataProvider); [BT_debugger showIt:self message:[NSString stringWithFormat:@"----Font: %@", fullName]]; } -------------------------------------- Sample of returned data: -------------------------------------- 2014-01-28 09:40:38.319 universal[3802:60b] Family name: Helvetica 2014-01-28 09:40:38.320 universal[3802:60b] Font name: Helvetica-Oblique 2014-01-28 09:40:38.321 universal[3802:60b] Font name: Helvetica-Light 2014-01-28 09:40:38.323 universal[3802:60b] Font name: Helvetica-Bold 2014-01-28 09:40:38.323 universal[3802:60b] Font name: Helvetica 2014-01-28 09:40:38.325 universal[3802:60b] Font name: Helvetica-BoldOblique 2014-01-28 09:40:38.325 universal[3802:60b] Font name: Helvetica-LightOblique -------------------------------------- Code used to change the font that failed. I tried inserting it in bt_viewController.m as well as appDelegate.m --------------------------------------- [[UINavigationBar appearance] setTitleTextAttributes: [UIFont fontWithName:@"Helvetica-Bold" size:10.0]];
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
01/29/14 01:26 PM (11 years ago)
Hey Smug, I followed your guide step by step, and still couldn't get it to work. It displays the system font in the top bar no matter what I do. I tried your guide to use a custom font, and it didn't work,, nor did trying to adjust the font size of an already installed system font. I also downloaded the sample app in the link shenry provided, and copy and pasted the code word for word into my appdelegate.m file, and still didn't change anything. Here is what I used with that method: ---------------------------------------- NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]; shadow.shadowOffset = CGSizeMake(0, 1); [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName, shadow, NSShadowAttributeName, [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]]; ----------------------------------------- I also tried replacing the font name above with the custom one that I added to my project when using your tutorial. Still nothing :( No errors or anything, just always shows the default system font. I have a refresh button on the left of my bar and a context menu on the right of the bar... that shouldn't make a difference though, right? Any ideas? I don't care about the font itself... I just want to change the size of it. Is there something in the buzztouch project that is setting up the font in the navbar, and is maybe being called afterwards and overriding any code that I'm adding to my appdelegate.m file?
 
shenry
Aspiring developer
Profile
Posts: 469
Reg: Jan 10, 2012
Orange County, ...
13,390
like
01/29/14 01:36 PM (11 years ago)
I'm out of ideas, but it sure sounds like something is overriding the code. I bet Smug can solve this mystery.
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
01/29/14 01:38 PM (11 years ago)
Thats kind of what I'm thinking. The sample project in the link you gave me runs fine and changes the font as expected, but when I pop it into my project, it does nothing :( Smug... would it be easier if I dropbox'ed you my project?
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
01/29/14 08:00 PM (11 years ago)
This is a bit odd… It seems like my 'transcription' is a bit dated now, in iOS 7… I did a search on the word 'font' in my latest iOS project, and the only 'BT' files I found it listed was: 1) BT_contextMenu.m 2) BT_viewUtilities.h 3) BT_viewUtilities.m And although you would think 'aha! BT_viewUtilities! It actually only concerns the Quiz… Not the system. That tells me one basic thing: The system font is default, and there is no code 'asking' for it… it's automatic. So what we're going to have to do is figure out the *right* 'method' call, 'find' where it needs to go, and implement it. I'm going to guess that it will involve something 'similar' but perhaps not exactly, like this: NSDictionary *smugAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor],NSForegroundColorAttributeName, [UIColor greenColor],NSBackgroundColorAttributeName,nil]; [[self.navigationController navigationBar] setTitleTextAttributes:smugAttributes]; I just gotta figure out 'where' it should go… the thing is, I'm embroiled in a fight with Android right now, and am not sure when I'll get back to the iOS side of things… I'll try to hurry, but until it's done, It'll be on my plate… Cheers! -- Smug
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
01/30/14 08:17 AM (11 years ago)
Thanks Smug, Yeah, as you can see above I tried a similar code, with no success. The funny thing is it worked in a blank project (non-BT). No problem on waiting a bit, I have to finish up the data entry for the app anyways :)
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
01/30/14 09:02 AM (11 years ago)
If David comes up for air he might chime in, and I'll be back into iOS soon, as I *finally* solved my Android problem, so I can button up this new plugin soon. iOS is already done, I just need to smooth the edges and write some serious documentation for it. Cheers! -- Smug
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
01/30/14 09:05 AM (11 years ago)
Cool, can't wait... always love your plugins :)
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.