Italic Font Is Not Working on XCode - ios

I have copy my ttf font (ABeeZee-Italic.ttf) from Google WebFonts into Supporting Files and added into plist :
and assign my Label with tag = 4 :
because I have this code to embed fonts into UILabel:
for (UILabel *customLabel in [[self view] subviews]) {
if (customLabel.tag == 1) {
[customLabel setFont:[UIFont fontWithName:#"Raleway-ExtraLight" size:21]];
} else if (customLabel.tag==2){
[customLabel setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:14]];
} else if (customLabel.tag==3){
[customLabel setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:11]];
} else if (customLabel.tag==4){
[customLabel setFont:[UIFont fontWithName:#"ABeeZee-Italic" size:12]];
}
}
it's all working like a charm, but the italic font is not loaded into my iPod (iOS 6.1.3). UILabel with tag 4 is the iOS system font. not ABeeZee-Italic.
I tried to change the tag number, but also failed. Is there any specific requirements for non-regular font to be embedded into iOS?
thank you
UPDATE #iPatel answer : here's how my FontBook looks like, it has same name...

Check what is real name of font (ABeeZee-Italic) in "FontBook" ? and write name of font which is in "FontBook".
And use such like
[myLabelName setFont: [UIFont fontWithName:#"Font Name From Font Book" size:32]];
More information about how to add custom font in app. then read This Answer.
EDITED:
Also try with
Select Your font from your project
right click
Open With External Editor
Install Font
select inserted font
And check what is "Full name", copy and past it in your code:
May be helpful :)

You can do this in two ways.
Using ttf file
[customLabel setFont:[UIFont fontWithName:#"Raleway-ExtraLight.ttf" size:21]];
or
Using font name
[customLabel setFont:[UIFont fontWithName:<Name of Raleway-ExtraLight> size:21]];
In your case just add .ttf in setFont: else get Name of font as shown in below image.
Just right click on .ttf file > Get Info > Full Name (your font name. You can use this name as font name in setFont)
EDIT
Check the full name here and paste it in your code.
[customLabel setFont:[UIFont fontWithName:#"ABeeZee Italic" size:21]];
EDIT 1
May be you need to Register Fonts. Just try to register and check.

Related

How do I set bold font on UILabel in iOS?

I want to set bold font on UILabel. I generate label by coding. when i try to set bold font on label ios set system regular font but i want to set 'Noto sans-Bold' font. In storyboard set font completely.
My code is
UILabel *lblName=[[UILabel alloc]initWithFrame:CGRectMake(0, 25, 100, 21)];
lblName.text=#"Hello World";
[lblName setFont:[UIFont fontWithName:#"Noto Sans-Bold" size:15.0]];
[self.view addSubview:lblName];
If you haven't got the formatting of your font title correct, it will automatically default back to system font.
The issue is with your font name and since Noto isn't native it's going to be difficult to know exactly what name you need to provide.
Make sure you have added that font to your project.
You can check your available fonts by running this code:
SWIFT
for familyName in UIFont.familyNames() {
print("\n-- \(familyName) \n")
for fontName in UIFont.fontNamesForFamilyName(familyName) {
print(fontName)
}
}
OBJECTIVE-C
for (NSString *familyName in [UIFont familyNames]){
NSLog(#"Family name: %#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"--Font name: %#", fontName);
}
}
lblname.font = [UIFont fontWithName:#"Noto Sans-Bold" size:18];
and one more thing check your font correctly it is available in list or not.
Here is a list of available fonts .
List of available fonts
EDIT:-
or if you want to set font which not available in xcode then you have to first install it in XCode
please follow this tutorial.
Install custom fonts
As said by Jilouc follow this procedure.
Add the font files to your resource files
Edit your Info.plist: Add a new entry with the key Fonts provided by application.
For each of your files, add the file name to this array
On the example below, I've added the font "DejaVu Sans Mono":
In your application you can
the use [UIFont fontWithName:#"DejaVuSansMono-Bold" size:14.f].
Or if you want to use it in html/css, just use font-family:DejaVu
Sans Mono;

Change default font to custom fonts in whole app

I want to keep custom fonts in iOS app. Currently, I'm using iOS default fonts.
Is there any possible way for the same?
How do I include the custom font so that I can use it?
Make a category:
#import "UILabel+Helper.h"
#implementation UILabel (Helper)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {
self.font = [UIFont fontWithName:name size:self.font.pointSize]; }
#end
Then in AppDelegate.m:
[[UILabel appearance] setSubstituteFontName:#"SourceSansPro-Regular"];
This will change font in whole app even on UIButton, UILabel, inside UITableViewCell, UICollectionViewCell, UIView, UIContainerView, or anywhere in application without changing the font point size.
This is memory efficient approach rather than enumerating all views in your UIViewController.
Copy your font file into resources
Add a key to your Info.plist file called UIAppFonts. ("Fonts provided
by application")
Make this key an array
For each font you have, enter the full name of your font file
(including the extension) as items to the UIAppFonts array
Save Info.plist
Now in your application you can simply call following method to get the custom font to use with your UILabel and UITextView, etc…
[UIFont fontWithName:#"CustomFontName" size:15];
Check which fonts are available in your resources :
func allFonts(){
for family in UIFont.familyNames(){
println(family)
for name in UIFont.fontNamesForFamilyName(family.description)
{
println(" \(name)")
}
}
}
Change font for whole application in UILabel :
Add code in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[UILabel appearance] setFont:[UIFont fontWithName:#"YourFontName" size:17]];
...
}
Change font in one viewConroller same as font size set in view design:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setFontFamily:#"YourFontName" forView:self.view andSubViews:YES];
}
-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
}
if (isSubViews)
{
for (UIView *sview in view.subviews)
{
[self setFontFamily:fontFamily forView:sview andSubViews:YES];
}
}
}
1- Drag & Drop Font file .ttf to your project
2- Verify the font is in the project.
By selecting the font, and verifying “Target Membership” in the Utilities area.
3- Add info in plist
4- In delegate add this code
[[UILabel appearance] setFont:[UIFont fontWithName:#"YourFontName" size:11.0]];
[[UITextField appearance] setFont:[UIFont fontWithName:#"YourFontName" size:11.0]];
[[UITextView appearance] setFont:[UIFont fontWithName:#"YourFontName" size:11.0]];
I hope this will help
Ok, from all the other answers you are now very well familier that how to add a custom font (other than iOS default font) into app.
So now what? One thing is still remaining, I'm not sure if you want to use the same size custom font everywhere. If so,
I'm using this way to have custom font in my app.
If your app will have a single custom font then you can use it like this way for entire app.
#define appFontBold(x) [UIFont fontWithName:#"CustomFont-Bold" size:x]
#define appFontLight(x) [UIFont fontWithName:#"CustonFont-Light" size:x]
#define appFontRegular(x) [UIFont fontWithName:#"CustonFont-Regular" size:x]
I've added these macros globally (so can access it from anywhere)
and you can use it like this, yourLabel.font = appFontRegular(12); or yourTextField.font = appFontBold(14); this helps you in following situations,
Whenever you needs to change the custom font, you only need to change font name in macros.
Your code will looks clean, no need write [UIFont fontWithName: size:]; everywhere.
If your app will have more than one custom font then you can use it like this way for entire app.
then also you can define different macros like this,
Then above macro will be the same (as this is the maximum used fonts in app), for other fonts :
#define appFontBoldOtherFont(x) [UIFont fontWithName:#"CustomOtherFont-Bold" size:x]
#define appFontLightOtherFont(x) [UIFont fontWithName:#"CustonOtherFont-Light" size:x]
#define appFontRegularOtherFont(x) [UIFont fontWithName:#"CustonOtherFont-Regular" size:x]
Add your custom font into your project , i.e. Dragged the font file(CALIBRIZ_0.TTF) into XCode project.
Edit Info.plist: Add a new entry with the key "Fonts provided by application".
For each of your files, add the file name to this array
Now set font to your label
yourLabel.font = [UIFont fontWithName:#"Calibri" size:15];
If you want to change all fonts for all UILabel in one place, you need to create a category of UILabel.
for example:
#import <UIKit/UIKit.h>
#interface UILabel (AppFont)
#end
#implementation UILabel (AppFont)
-(void)awakeFromNib
{
self.font = [UIFont fontWithName:#"yourCustomFont" size:self.font.pointSize];
}
#end
after you've created the category, you will need to import this class for all the file, or you can add it to '.pch' file for your project.
same solution for buttons / textview ..

Detecting light fonts in iOS

I am trying to specify the font family of every label in my iOS app in a way that makes it fairly easy to change them later. I don't want to have to go through Interface Builder and reset every font on every screen. According to this post, I have created a method that will find all the fonts in a view and set them accordingly.
In my case, there are a few different font families I need to use based on whether the font is bold, italic, or light (e.g. skinny). These are all located in separate files such as "OpenSans-Semibold.ttf", "OpenSans-Italic.ttf", and "OpenSans-Light.ttf".
Ideally, I would like to be able to set the font to bold, italic, or light in Interface Builder, then have the code override just the font family, using the appropriate .ttf file. According to this post, I can pretty easily detect whether the font has been set to bold or italic, but finding out whether it's light or not doesn't seem to be working.
For the light fonts, the value of "traits" is 0x0--so no flags are set. Is there another way to detect light fonts?
Code looks like this:
- (void) setFontFamily:(UIView*)view
{
if([view isKindOfClass:[UILabel class]])
{
UILabel* label = (UILabel*)view;
UIFontDescriptorSymbolicTraits traits = label.font.fontDescriptor.symbolicTraits;
BOOL bold = traits & UIFontDescriptorTraitBold;
BOOL italic = traits & UIFontDescriptorTraitItalic;
if(bold)
[label setFont:[UIFont fontWithName:#"OpenSans-Semibold"size:label.font.pointSize]];
else if(italic)
[label setFont:[UIFont fontWithName:#"OpenSans-Italic"size:label.font.pointSize]];
else if(light)
[label setFont:[UIFont fontWithName:#"OpenSans-Light"size:label.font.pointSize]];
else
[label setFont:[UIFont fontWithName:#"OpenSans"size:label.font.pointSize]];
}
for(UIView* subView in view.subviews)
[self setFontFamily:subView];
}
Your entire approach to determining a font based on its characteristics is problematic:
else if(italic)
[label setFont:
[UIFont fontWithName:#"OpenSans-Italic"size:label.font.pointSize]];
You are hard-coding the font name based on the trait. Instead, ask the runtime for the font based on the name and trait. In this very simple example I find out what installed font, if any, is an italic variant of Gill Sans:
UIFont* f = [UIFont fontWithName:#"GillSans" size:15];
CTFontRef font2 =
CTFontCreateCopyWithSymbolicTraits (
(__bridge CTFontRef)f, 0, nil,
kCTFontItalicTrait, kCTFontItalicTrait);
UIFont* f2 = CFBridgingRelease(font2);
Note that this code is valid in iOS 7 only, where CTFontRef and UIFont are toll-free bridged to one another. In theory it should be possible to do this without C functions through UIFontDescriptor, but the last time I looked it was buggy and didn't work for all fonts (e.g. Gill Sans!).
That is what you should be doing: determine the symbolic and weight traits of your starting font, and then ask the runtime for the font that most close matches your requirements.

UILabel - set custom fonts

I want to add GillSans-Bold font to a UILabel.
I have set it in the xib file , and I'm also setting it up in my class as follows :
[label setFont:[UIFont fontWithName:#"GillSans-Bold" size:18]];
But , it doesn't seem to work for me.
Any suggestions ?
iPhone 4.3 doesn't have Gill Sans, but iPad has it since 3.2.1.
See this list comparing fonts for iPad 4.3 and iPhone 4.3. To be sure, this is how you get the list of fonts available on your device:
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"%#", fontName);
}
}
If it says
GillSans
GillSans-Bold
GillSans-BoldItalic
GillSans-Italic
then [UIFont fontWithName:#"GillSans-Bold" size:18] should return a valid font.
For this to be working I had to add this font in my project directory , and added this font in the Info.Plist file
Does the font GillSans-Bold exist? Check if [UIFont fontWithName:#"GillSans-Bold" size:18] returns an UIFont, not null.
Swift
let label = UILabel()
label.font = UIFont(name: "Roboto-Regular", size: 16)
label.text = "Your text here"
P.S. Before that you must:
Add custom font in project
Add font names in Info.plist file
Add font files in Copy Bundle Resources
After that you can also use this fonts in storyboards

make UILabel's text bold

I want to make UILabel's text bold
infoLabel=[[UILabel alloc]initWithFrame:CGRectMake(90,150, 200, 30)];
[infoLabel setText:#"Drag 14 more Flavors"];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont fontWithName:#"Arial" size:16]];
[infoLabel setTextColor:[UIColor colorWithRed:193.0/255
green:27.0/255
blue:23.0/255
alpha:1 ]];
If you want to retain the system font and make it bold:
[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];
Try
[infoLabel setFont:[UIFont fontWithName:#"Arial-BoldMT" size:16]];
It may also be worth checking if the font you're trying to use is available on device
Using the GUI in Xcode select the label then go to the Attributes Inspector. One of the options is Font. Click on the font icon (not the up-down arrows). In the popup that appears expand the Font ComboxBox. Under the Bold System section choose Regular.
For swift users this should work:
myLabel.font = UIFont.boldSystemFont(ofSize: 12.0)
or if you'd like to use a different font:
myLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 12.0)
Where possible I would suggest using dynamic font sizes to provide the best possible accessibility to your users.
You can make a label use a system dynamic font and set it to have bold text by doing the following:
exampleLabel.font = UIFont.preferredFont(forTextStyle: .body, compatibleWith: UITraitCollection(legibilityWeight: .bold))
You can set the stroke with a negative value to make a bold effect if you don't have the bold variation of your custom font. See here:
How can I both stroke and fill with NSAttributedString w/ UILabel

Resources