How to set font on Title of Button Objective-C - ios

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'

Related

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

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

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

Formatting UITableView Cells in iphone

I am developing an iphone app. In the image below I want the first name to be in plain font and the last name to be bold.. How can I do that? Please suggest me.. Please check this image:
Another questio..Now I think the reverse way but the problem here is the first line and second line you see are part of the same string. I want the first line to be bold and the second line to be in plain font. I am storing this in a dictionary. So my dictionary has a key and the value is a string of names and departments. I am unable to set the font. I tried to create two labels and tried to split the string according to the index and assign it to the labels I created. But, in this case the index keeps on changing as there might be a first name for a contact or there might not be any name.
In this case Prinicipal should be in plain font and name should be in bold
Please see the below image:
Starting OS 6.0 there is a property on the UILabel called attributedText which has NSAttributedString type (Available in iOS 3.2 and later.).
Here it is how I am using it:
NSDictionary *firstNameAttributes = #{ NSFontAttributeName : [UIFont fontWithName:#"Helvetica" size:18.0],
NSStrokeColorAttributeName : [UIColor blackColor]};
NSDictionary *lastNameAttributes = #{NSFontAttributeName : [UIFont fontWithName:#"Helvetica-Bold" size:20.0],
NSStrokeColorAttributeName : [UIColor blackColor]};
NSString* first = ... first name ..;
NSString* last = [NSString stringWithFormat:#" %#",... last name ....];
NSMutableAttributedString * name =
[[NSMutableAttributedString alloc] initWithString:first attributes:firstNameAttributes];
NSMutableAttributedString * lastName =
[[NSMutableAttributedString alloc] initWithString:last attributes:lastNameAttributes];
[name appendAttributedString:lastName];
[[cell textLabel] setAttributedText:name];
See also Introduction to Attributed String Programming Guide.
Since the label that you're showing the name string in defines the formatting and style, if you want to have different styles you need to have a different uilabel for each each style you want. Specifically, you will need a uilabel for the firstname: firstNameLabel.font = [UIFont systemFontOfSize:12]; and one for the lastname: lastNameLabel.font = [UIFont boldSystemFontOfSize:12];.
First place the first name string is the firstNameLabel then call [firstNameLabel sizeToFit] to fit the label text within it. Then use the frame of the firstNameLabel to place the lastNameLabel directly after it.
UILabel * firstNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,25)];
firstNameLabel.tag = firstNameLabelTag //This should be a constant probably
firstNameLabel.font = [UIFont systemFontOfSize:12];
firstNameLabel.text = theStringRepresentingTheFirstName;
[firstNameLabel sizeToFit];
UILabel * lastNameLabel = [[UILabel alloc] initWithFrame:
CGRectMake(10+firstNameLabel.frame.size.width+2, 10, 100, 25)];
lastNameLabel.tag = lastNameLabelTag //This should be a constant probably
lastNameLabel.font = [UIFont boldSystemFontOfSize:12];
lastNameLabel.text = theLastNameString;.
[cell.contentView addSubview:firstNameLabel];
[cell.contentView addSubview:lastNameLabel];
And as for splitting the name string, you're probably pretty limited there. I would split on the first space and assume the first string is the last name (as in your first picture).
The principle case is similar, you need a label for each style that you want to present.
- (IBAction)buttonPressed:(UIButton *)sender {
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *plainText = [NSString stringWithFormat:#"%# button pressed.", title];
NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc] initWithString:plainText];
NSDictionary *attributes = #{ NSFontAttributeName : [UIFont boldSystemFontOfSize:self.statusLabel.font.pointSize]};
NSRange nameRange = [plainText rangeOfString:title];
[styledText setAttributes:attributes range:nameRange];
self.statusLabel.attributedText = styledText;
}
You have to create a custom UITableViewCell. http://icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/

Resources