Adding a custom font to a bar button item - ios

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]};

Related

Objective-C syntax error "Expected ']'"

I am attempting to configure cells in a TableView. One of the things I am trying to do is change the font and color of the text in the cells. Here is the code:
- (void)configureView {
UIFont *cellFont = [UIFont fontWithName:#"Raleway-Thin" size:14];
NSDictionary *cellAttributesDictionary = [NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: cellFont];
}
At the colon after NSFontAttributeName I am getting the error Expected ']'. What is causing this error?
The proper syntax for a dictionary literal in Objective-C is #{...};
NSDictionary *cellAttributesDictionary = #{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: cellFont};

UINavigationBar setTitleTextAttributes with UnderLine Text

I am trying to setTextAttribute with UnderLine in all Views using this code
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor redColor],
// NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle),
(NSString *) kCTUnderlineStyleAttributeName:#(kCTUnderlineStyleDouble),
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Bold" size:17]}];
but its not working , I know another approach using creating custom UILabel with NSAttributedString and setting it on TitleView but is there any other way to achieve this using appearance protocol?
NSUnderlineStyleAttributeName should do that job for you.
Try this Dude:
NSDictionary *attributes = #{
NSUnderlineStyleAttributeName: #1,
NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-Bold" size:17]
};
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
Updates:
I refered the Apple's documentation, It seems to be not possible through appearance protocol.
It says
"You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary, using the text attribute keys described in NSString UIKit Additions Reference."
I tried by creating the new simple project as well. Couldn't see the line.
Apple Documentations:
UINavigationBar
NSString Keys for Text Attributes Dictionaries

ios custom font in [UINavigationController appearance] titletextapperance crashes app

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);
}

NSAttributed String Won't change font [duplicate]

This question already has answers here:
UITextField attributedPlaceholder has no effect
(4 answers)
Closed 8 years ago.
I'm trying to format the placeholder text of a UITextField using an NSAttributedString. This code works successfully for the foreground color and kerning attributes. However, it won't change the font or the font size. What am I doing wrong? Thanks!
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:
#"USER NAME"
attributes:#{
NSForegroundColorAttributeName: [UIColor redColor],
NSFontAttributeName : [UIFont fontWithName:#"Georgia" size:8.0f],
NSKernAttributeName : [NSNumber numberWithFloat:3.0f]
}];
self.userNameTextField.attributedPlaceholder = [aString copy];
For me it works when I set custom font for UITextField, and then set other attributes for placeholder. My example that works:
_searchField.font = [UIFont fontWithName:fontRokkitRegular size:20];
UIColor *color = [UIColor colorWithRed:150.0f/255 green:150.0f/255 blue:150.0f/255 alpha:1];
_searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Keyword search" attributes:#{NSForegroundColorAttributeName:color}];
It looks like the placeholder font size is governed by the UITextField's font so I think you will have to subclass UITextField and add:
-(void)drawPlaceholderInRect:(CGRect)rect
{
NSString *aString = #"USER NAME";
[aString drawInRect:rect withAttributes:#{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName : [UIFont fontWithName:#"Georgia" size:8.0f], NSKernAttributeName : [NSNumber numberWithFloat:3.0f]}];
}

Attributed UITextView doesn't work with Korean symbols

Attributed UITextView doesn't work with Korean symbols. Steps to reproduce:
Add UITextView to the form.
Use the following code:
NSDictionary *attributes = #{
NSFontAttributeName:[UIFont fontWithName:#"HelveticaNeue" size:15],
NSForegroundColorAttributeName:[UIColor blackColor]};
[textView setAttributes:attributes
range:NSMakeRange(0, textView.text.length)];
Run the application, type any text on Korean (에서 보냄) and tap or press Enter.
The Korean text will disappear or will be replaced by several trash symbols. Why? How can I fix it?
P.S. The is an interesting answer on the question UITextField text disappears on every other keystroke But I'm creating UITextView object on the code.
Use This code it will help you.
//[_txtViewChallangeDescription setBackgroundColor:[UIColor redColor]];
_txtViewChallangeDescription.font = [UIFont fontWithName:kFontHelvetica size:kFontSize14];
[_txtViewChallangeDescription setTextColor:[UIColor blackColor]];
[_txtViewChallangeDescription setEditable:NO];
_txtViewChallangeDescription.delegate=self;
_txtViewChallangeDescription.scrollEnabled=NO;
_txtViewChallangeDescription.textContainer.lineFragmentPadding = 0;
_txtViewChallangeDescription.textContainerInset = UIEdgeInsetsZero;
NSDictionary *attrDict = #{
NSFontAttributeName : [UIFont fontWithName:kFontHelvetica size:kFontSize14],
NSForegroundColorAttributeName :[UIColor colorWithRed:152.0/255.0f green:132.0/255.0f blue:43.0/255.0f alpha:1.0f]
};
[_txtViewChallangeDescription setLinkTextAttributes:attrDict];
The following incorrect code works fine:
NSDictionary *attributes = #{
NSFontAttributeName:[/*UIFont fontWithName:#"HelveticaNeue" size:15],*/
NSForegroundColorAttributeName:[UIColor blackColor]};
[textView addAttributes:attributes
range:NSMakeRange(0, textView.text.length)];

Resources