UINavigationBar custom font not working on iOS 8 - objective-c

I've spent some hours trying to use a custom font for the navigation bar title on iOS 8. This works ok on iOS 7:
UIFont *font = [UIFont fontWithName:#"Gotham-Bold" size:12];
NSDictionary *textAttributes = #{ NSFontAttributeName: font };
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
But on iOS 8 the text simply disappears. Changing the color, or the system font size works perfectly, but trying to use a custom font won't work at all. Anyone has any idea on this?
Thanks!
EDIT
I've created a small project so you can try it yourself:
https://github.com/noquepoaqui/customHeaderFont
These lines of code are on MasterViewController on line 30.

You have a typo when you assign the attributes:
It should be:
self.navigationController.navigationBar.titleTextAttributes
instead of:
self.navigationController.navigationBar.TitleTextAttributes
If that doesn't fix the issue, you can test if the font was added correctly by placing this code in the AppDelegate's didFinishLoadingWithOptions. This will print all available fonts of your app. Just look at the output window and search for your font name (Gotham-Bold). If it's not listed you can delete the font from your project and add it again via drag & drop. Make sure to tick "add to target" next to your app in the dialog that appears.
//list all available fonts
for (NSString *family in [UIFont familyNames]) {
NSLog(#"---------- %# ----------", family.uppercaseString);
NSArray *names = [UIFont fontNamesForFamilyName:family];
for (NSString *font in names) NSLog(#"%#", font);
}

The .ttf font i was using was not working well. I've installed the .otf version and everything works well.

Related

Force Lucida Grande Font in a Yosemite Cocoa app

I have a requirement for some labels in my app to use Lucida Grande. When I specify this font in Xcode it renders correctly in the xib, but when you launch the app in Yosemite it switches back to Helvetica Neue.
How can I get this to work correctly?
As per Ken's advice, setting the font manually in the awakeFromNib() method gives the correct result.
You can either set the font property of the NSTextField
[self.myLabel setFont:[NSFont fontWithName:#"Lucida Grande" size:50.f]];
or use an NSAttributedString
NSDictionary *attributes = #{ NSFontAttributeName : [NSFont fontWithName:#"Lucida Grande" size:50] };
NSAttributedString *text = [[NSAttributedString alloc] initWithString:#"Hello" attributes:attributes];
[self.myLabel setAttributedStringValue:text];
Interestingly, in many cases I have seen fonts with spaces in the font name input with dashes, i.e. #"Lucida-Grande", this actually breaks the output, dashes must be omitted.

Impossible to change font of cell.textLabel in ios8

I'm not able to change the textLabel's font in UITableViewCell. This line doesn't work with iOS 8 SDK : [cell.textLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:16.5]];
Can anyone give help me ?
EDIT : My app run with iOS7. I want to test it with iOS8. Only this line of code doesn't work.
I find the solution. This line:
[cell.textLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:16.5]];
change to this line :
[[cell textLabel] setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size: 16.5]];
This is the only change I made in my code.
I'm not familiar with xCode6, but you should check if the file is set to objective-c.
I had the same problem and was able to resolve it.
try opening your ttf file in the font book application and validate it. if you get a red X in one of the validation steps that might be the issue.
I replaced the ttf with a file from a different font and the problem was resolved.

ios 7 UIBarButtonItem UIAppearance not setting font

I'm using this piece of code to set the default font (Custom) for all my UIBarButtonItems:
NSDictionary *attributesBarButtonItem = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"ProximaNova-Light" size:18.0], NSFontAttributeName, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributesBarButtonItem forState:UIControlStateNormal];
NSLog(#"%#", [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]);
However, it seems to be ignored as the font does not change, and NSLog returns (null). It's a bit confusing because its pretty much the same code I'm using to set the default font for all my navigation bars and it works fine for them.
The piece of code is placed in AppDelegate´s didFinishLaunchingWithOptions but I've also test it in other viewControllers (viewDidLoad) with exact same result.
Other strange behaviour I've noticed:
I've got a tab bar controller, and when I load any viewController with bar button items it doesn't work, but if I push another viewController it works (The font is changed to the selected one), and it keeps working even if that viewController is popped out, although it will stop working if another tab is pushed.
Any help to try to set a default font for the UIBarButtonItems would be appreciated. Thanks!
Is this your custom font ?
There could be few problems:
is the font in TTF format ?
if you click on the font in xcode is Target membership in right panel checked ?
did you add the font to project plist file ?
Also you should use UITextAttributeFont in the dictionary:
[UIBarButtonItem appearance] setTitleTextAttributes:#{UITextAttributeFont:[UIFont fontWithName:#"ProximaNova-Light" size:18.0]} [forState:forState:UIControlStateNormal];
I had a similar issue arise because I was creating the leftBarButtonItem before I set the appearance attributes. Swapping the order such that appearance was set first fixed the problem.

Changing Font in UITextView does not work in iOS7

I have a UITextView with text in it, the View is in a UITableViewCell. I noticed that the font was not quite the same on iOS7 as with iOS6, noting it was set to "system" I decided to specify the exact font/size.
It appeared nothing happened so I thought I would do a better test (big font not used anywhere), like this in my "CellForRowAt....";
cell.newsItemDescription.font = [UIFont fontWithName:#"didot" size:20];
cell.newsItemDescription.text = newsDescriptions[indexPath.row];
In iOS6 it comes out like this;
In iOS7 it comes out like this;
It happens in just a few places in the app but it is very annoying, can't figure out why? I am fast getting to the point where I may use the iOS7 Font/ Size throughout the app.
Some extra info;
The UITextView is resized per cell along with the cell (using springs/struts, i.e. no Auto Layout) and HeightForRow...
The font was setup in Storyboard originally (as system)
This is the same on devices and Simulator
I have a strange behavior in iOS 7. Font is smaller than I expect, if I was set it in to the xib.
If I set font after setting the text it's works for me. Otherwise font is smaller.
Try this:
cell.newsItemDescription.text = newsDescriptions[indexPath.row];
cell.newsItemDescription.font = [UIFont fontWithName:#"didot" size:20];
I had the same issue today with UITextView, and while reading AlKozin's answer, I remembered something: somewhere, sometime I read that since iOS 7, the best practice to set font styles is to set them after the View has loaded. In other words, if I set everything in viewDidLoad: , nothing happens. You have to set up the font of your UITextView in viewWillAppear:, like this:
-(void)viewWillAppear:(BOOL)animated
{
self.myTextView.font = [UIFont fontWithName:#"Baskerville-Italic" size:18.0];
}
I ran into a weird bug (Xcode 7.2.1), where unchecking "Selectable" in IB was causing the UITextView to not adhere to the font settings specified through IB.
This is same issue I was facing.
Making UITextview "selectable" from storyboard will work.
Setting textview.font worked fine for me on iOS 7, but none of these answers, or other similar answers on other SO pages worked for me on iOS 8. The only way I was able to get it working was to use an NSAttributedString.
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
[pStyle setAlignment:NSTextAlignmentCenter];
tv.attributedText = [[NSAttributedString alloc]
initWithString:text
attributes:#{ NSForegroundColorAttributeName: color,
NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
NSParagraphStyleAttributeName: pStyle,
} ];

Setting size to custom iOS font doesnt work

I am using some custom fonts in my iphone app. They are displayed correctly, but i cant change their size.
Here is how set this font for UILabel:
myLabel.font=[UIFont fontWithName:#"Cabin-Regular" size:18];
This label is part of uitableview cell, if that could play any role.
Label settings in IB:
I change only color, text and font in code for this label.
What could be wrong?
Are you sure the font is loaded correctly?
You can try:
for (NSString *familyName in [UIFont familyNames]) {
NSLog(#"%#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"\t%#",fontName);
}
}
This will show you all the fonts that are loaded. Make sure the same name "Cabin-Regular" is showing up exactly the same. A lot of times the font itself isn't loaded and then the font-size won't be used at all.
You need to add your font in your Xcode, if you are adding manually.
Add your font in your project files
Go to your Project name-Info.plist file, right click and click Add Row.
In your new row type Fonts provided by application. If you expand it you can add new items, in there add value to item 0 . The value should be your font file name. like below image
4. Double click on your font, it'll open in external editor, like following image
5.Give that name into your fontWithName: method. If you are setting value to your tableviewcell. the code will be
cell.nameLbl.font = [UIFont fontWithName:#"Cabin" size:18.0];
It works fine for me. I hope it helps :)