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

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

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'

iOS Change system font to custom font globally

I want to change all system font to custom font at one place with storyboard and xib.
I have tried below code to change font.
[[UILabel appearance] setFont:[UIFont fontWithName:#"fontName" size:17.0]];
[[UITextField appearance] setFont:[UIFont fontWithName:#"fontName" size:17.0]];
When I used above code its working fine but its changing size also.
I just want to change font family and keep size as it has in storyboard or xib.
Please provide me some good solution.
I think you can keep using the same statements, but instead of setting the size using a literal of your choice, just use the current system font size like this:
float currentSystemFontSize = [UIFont systemFontSize];
[[UILabel appearance] setFont:[UIFont fontWithName:#"fontName" size:currentSystemFontSize]];
This should keep the system's font size and only change the font family.
Check this answer for more information about how to get system font size. Good luck!
call below methods in ViewDidLoad():
[self setCustomFontFamilyNotSize:#"CustomFontName" forView:self.view andSubViews:YES];
Method Dealarations:
-(void)setCustomFontFamilyNotSize:(NSString*)customFontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:customFontFamily size:[[lbl font] pointSize]]];
}
if ([view isKindOfClass:[UITextField class]])
{
UITextField *txt = (UITextField *)view;
[txt setFont:[UIFont fontWithName:customFontFamily size:[[txt font] pointSize]]];
}
}

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 change font size of label without changing font family in xcode

how to change font size of label without changing font family in xcode6 ?I am using following line of code:
lblMain.font = [UIFont systemFontOfSize:22];
Here's an example:
lblMain.font = [UIFont fontWithName:#"HelveticaNeue" size:22];
Or - you can even use the current font:
lblMain.font = [lblMain.font fontWithSize:22];
NSString *fontName = lblMain.font.fontName;
CGFloat fontSize = lblMain.font.pointSize;
[lblMain setFont:[UIFont fontWithName:fontName size:NEWSIZE]];
Use following code, if you just want to change font size keeping the same font family:
lblMain.font = [UIFont fontWithName:lblMain.font.fontName size:22.0f];
OR
You can also use this code:
lblMain.font = [lblMain.font fontWithSize:22.0f];
[lblMain setFont:[UIFont fontWithName:#"HelveticaNeue" size:17]];

creating bold text in pdf from ipad

I have an iPad app that creates a pdf and works fine however I am trying to get the headings bold.
Currently I have:
UIFont *font = [UIFont fontwithname:#"arial" size 12];
I would like to know what I need to add to make the text bold as well.
Many thanks
This does the trick:
UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:12.0];

Resources