ios custom font in [UINavigationController appearance] titletextapperance crashes app - ios

I have an application that is using a custom font. I am having issue setting the font of the nav bar with the Appearance property.
I have added the fonts to my project, and added them the my info.plist file as well.
Here is my ode to set the font:
[[UINavigationBar appearance] setTitleTextAttributes: #{NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [Refrigerator boldFontWithSize:24.0f]}];
Here is my custom Refridgerator object for managing fonts:
+ (UIFont *)boldFontWithSize:(NSInteger)size {
return [UIFont fontWithName:#"RefrigeratorDeluxe-Bold" size:size];
}
And here is a screenshot of where I got the "proper" font name from Font Book:
The error I get is this:
[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]
If I change this to something simple like "systemFont" then the error does not occur.

You have to add the font files to your Info.plist with the UIAppFonts key.
You could also check the spelling of the font name using;
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"family: '%#' font: '%#'", familyName, fontName);
}
}
or
for (NSString *fontName in [UIFont fontNamesForFamilyName:#"RefrigeratorDeluxe"]) {
NSLog(#"font: '%#'", fontName);
}

Related

How to set font on Title of Button Objective-C

I am trying to set the font of a title on a button. I read from Apples Documentation on Fonts in Objective C and tried to implement but its not changing the font . I also tried this post... setting UIButton font. I got my font from google fonts..."Nunito-Black.tff"
Here is my code below,
- (void)addHeaderButton {
_headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.headerButton.frame = self.bounds;
self.headerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.headerButton setTitleColor:[UIColor colorWithRed:(0.0/255.0) green:(51.0/255.0) blue:(102.0/255.0) alpha:(1.0)] forState:UIControlStateNormal];
self.headerButton.titleLabel.font = [UIFont fontWithName:#"Nunito-Black" size:25];
//[self.headerButton setFont:[UIFont systemFontOfSize:25]];
[self.headerButton addTarget:self action:#selector(headerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.headerButton];
}
Here is the steps for adding custom fonts into your app.
Step : 1
Add your custom fonts to your app bundle and your Info.plist with UIAppFonts (Fonts provided by application) key. It's look like below image
Step : 2
Get the custom added font name using below snippet
[Objective-C]
for (NSString *familyName in [UIFont familyNames]){
NSLog(#"Family name: %#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"--Font name: %#", fontName);
}
}
[Swift - 3]
for family in UIFont.familyNames {
print("\(family)")
for name in UIFont.fontNames(forFamilyName: family) {
print(" \(name)")
}
}
Step : 3
Get the name of font from above snippet and after getting the name you can set the font name to your respected element as below
self.headerButton.titleLabel.font = [UIFont fontWithName:#"Nunito-Black" size:25];
You need to add the exact name of font to Info.plist under 'Fonts provided by Application'

Change the font size and font style of UISearchBar UISegmentedControl in iOS 7

How can I change the font of the UIButtons in the scope bar of a UISearchBar?
The font of the UISearchBar input I have already changed but this does not change it for the scope bar.
With this code I changed the UISearchBar text font and color in my AppDelegate.m.
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:FONT_COLOR, NSFontAttributeName:NAVIGATION_BAR_FONT}];
u can use this code:
UIFont *font = [UIFont fontWithName:#"ur font name" size:10];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
[your segmentName setTitleTextAttributes:attributes forState:UIControlStateNormal];

Adding a custom font to a bar button item

I am attempting to add custom font to a bar button item but I am getting this crash.
-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
I'm pretty sure I have added the font correctly, it is in my info.plist and the custom font shows up in IB.
This is how I am setting the font:
NSDictionary *barButtonAppearanceDict = #{NSFontAttributeName : [UIFont fontWithName:#"OpenSans-Regular" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance]setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
Any ideas as to why I am getting this error?
This is how I am adding the font.
To add a custom font to your application, here is a useful guide:
http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
You can use this tutorial to double-check your custom font.
Whatever the method you use to add a custom font, run the below code to check the name of the font as it is loaded in your app. It will list all the fonts that are available in your app.
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
Then, you can get the name of the font in the system.
I don't know how you add your custom font, but in general, the Regular font is loaded without Regular suffix.
So, replace
NSDictionary *barButtonAppearanceDict = #{NSFontAttributeName : [UIFont fontWithName:#"OpenSans-Regular" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
with
NSDictionary *barButtonAppearanceDict = #{NSFontAttributeName : [UIFont fontWithName:#"OpenSans" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};

How to check if UIFont is system font?

I have the following code for that creates multiple fonts.
UIFont* systemFont1 = [UIFont systemFontOfSize:12.0];
UIFont* systemFont2 = [UIFont boldSystemFontOfSize:12.0];
UIFont* systemFont3 = [UIFont italicSystemFontOfSize:12.0];
UIFont* customFont1 = [UIFont fontWithName:#"HelveticaNeue-Light" size:12.0];
UIFont* customFont2 = [UIFont fontWithName:#"HelveticaNeue-Regular" size:12.0];
UIFont* customFont3 = [UIFont fontWithName:#"HelveticaNeue-Thin" size:12.0];
UIFont* customFont4 = [UIFont fontWithName:#"MyriadPro" size:12.0];
UIFont* customFont5 = [UIFont fontWithName:#"MyriadPro-Italic" size:12.0];
UIFont* customFont6 = [UIFont fontWithName:#"MyriadPro-Condensed" size:12.0];
I would like to know which UIFont's are system. I practically need a method that would return a BOOL YES for variables: systemFont1, systemFont2, systemFont3 and NO for customFont4, customFont5, customFont6.
Since Helvetica Neue is system font on iOS7, this is subject of a debate, whether it should return NO or YES in these cases, but for my issue, it would be fine either way.
So my question is:
How to verify if UIFont instance was created by either of system font methods?
Thank you for your help!
Here is your method you want:
-(BOOL)isSystemFont:(UIFont *)font
{
return ([[font familyName] isEqualToString:[[UIFont systemFontOfSize:12.0f] familyName]])?YES:NO;
}
Or as an extension in Swift3
extension UIFont {
func isSystemFont() -> Bool {
return self.familyName == UIFont.systemFont(ofSize: 12.0).familyName
}
}
Above method will return as you need
if([self isSystemFont:systemFont1]) NSLog(#"SystemFont");
else NSLog(#"Custom Font");
if([self isSystemFont:customFont1]) NSLog(#"SystemFont");
else NSLog(#"Custom Font");
Output is
2014-03-04 15:48:18.791 TestProject[4031:70b] SystemFont
2014-03-04 15:48:18.791 TestProject[4031:70b] Custom Font
Fonts consist of more than name and size. Here's a more thorough check. Caveat: only work on iOS 7.
BOOL FontIsEqualToFont(UIFont *font1, UIFont *font2)
{
return [[[font1 fontDescriptor] fontAttributes] isEqual:[[font2 fontDescriptor] fontAttributes]];
}
// use as follows:
BOOL isSystemFont = FontIsEqualToFont(systemFont1, customFont1);
Another good way to uniquely identify fonts is to use their postscript names:
BOOL fontsAreEqual = [[[font1 fontDescriptor] postScriptName] isEqualToString:[[font2 fontDescriptor] postScriptName]];
Just check fontName and/or familyName?
NSLog(#"Fontname: %#", systemFont1.fontName);
NSLog(#"Familyname: %#", systemFont1.familyName);
I have created method and tested in both iOS 6 and iOS7. It is working fine. I was getting result what you are expected.
Here is Code:
- (BOOL)isSystemFont:(UIFont*)pFont {
NSLog(#"pFont-%#",pFont.familyName);
// Object created for comparing system font with custom font
UIFont *font = [UIFont systemFontOfSize:10];
NSLog(#"Font-%#",font.familyName);
if ([pFont.familyName isEqualToString:font.familyName]) {
return YES;
} else {
return NO;
}
}
Hope, It will may helpful to you.

How to iOS adding custom font with same name but different weight?

I have a problem of adding custom font with same name but different weight like:
[_lblFullName setFont:[UIFont fontWithName:#"Museo Sans 500" size:20]];
[_lblTicket setFont:[UIFont fontWithName:#"Museo Sans 900" size:12]];
I can only make this work by adding only one font weight but i have to rename it Museo Sans and only import one font to bundle and delete the other font. like:
[_lblFullName setFont:[UIFont fontWithName:#"Museo Sans" size:20]];
[_lblTicket setFont:[UIFont fontWithName:#"Museo Sans" size:12]];
But it only allows me one font weight, I need to have to import this two font weight.
Please help.
[_lblFullName setFont:[UIFont fontWithName:#"MuseoSans-500" size:20]];
[_lblTicket setFont:[UIFont fontWithName:#"MuseoSans-900" size:12]];
You can access the names of your custom fonts in the "Museo Sans" family by running:
NSArray *fonts = [UIFont fontNamesForFamilyName:#"Museo Sans"];
for (NSString *fname in fonts) {
UIFont *font = [UIFont fontWithName:fname size:12.0];
NSLog(#"name: %# font: %#", fname, font);
}
Whichever string is logged as the name works when used with fontWithName.
In Swift 2.2, you may want to do like this:
let fonts = UIFont.fontNamesForFamilyName("Museo Sans")
for name in fonts {
let font = UIFont(name: name, size: 12)
print("name: \(font?.fontName) font: \(font)")
}

Resources