UILabel set bold font with sans-serif - ios

I am trying to set bold font for UILabel as follows :
lblActivities.font = [UIFont fontWithName:#"sans-serif-Bold" size:15.0];
But this is not appearing as bold.
Any suggestions?

1-You need your font in .otf or .ttf copied to your project. For example in Supporting Files.
2 - You need to edit .plist file.
3 - Add "Fonts provided by application" key into your plist and in Item 0 copy the exact filename of the font you copied to your Supporting files WITH extension. For example: "JosefinSansStd-Light_0.otf"
4 - Make sure that the font you imported to your app is being packed into app itself. Do that by selecting your Target, then Build Phases, then Copy Bundle Resources. If you don't see your font in there, drag it from Supporting Files.
5 - Finally, you would like to list all your fonts when the app starts just to see useable name for your font. You will do that with this little piece of code:
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (#"%#: %#", fontFamily, fontNames);
}
Search for your font in printed results, for example, I would search for "Josefin" and I would see that actual font name is "JosefinSansStd-Light". After that you only need to use that font by:
UIFont *customFont = [UIFont fontWithName:#"JosefinSansStd-Light" size:20];
In iOS8 you add your fonts directly to the project and they are visible in the interface builder. Modify your code to account for this but programmatically setting font for iOS7 and selecting it in xCode6 interface builder. PS. Interface builder in xCode6 gives you the correct font name that you can copy-paste into the code below.
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
if(SYSTEM_VERSION_LESS_THAN(#"8.0"))
{
UIFont *customFont = [UIFont fontWithName:#"OpenSans-Light" size:32];
self.registerLabel.font = customFont;
}
Hope this helps, cheers.
For More Information check This Post

you have direct option in Xcode
1. select the label on view controller
2. on your right hand side panel select attribute inspector .
3. select font text-box link
4. a alert box appear select font dropdownlist
5 select custom from that list
6 set your style bold from style dropdown

try this may be help you. Make sure the font name is correct or available in your application.
[lblActivities setFont:[UIFont fontWithName:#"sans-serif-Bold" size:15.0]];
The Font which you describe is not available in apple library by Default you have to use custom font for this.
see Below link
Using custom font in a UIWebView

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;

Custom font set in a storyboard won't display on device [duplicate]

This question already has answers here:
LaunchScreen.xib not displaying my custom font
(2 answers)
Closed 7 years ago.
I created a label in LaunchScreen.storyboard and used a custom font. It looks all right in xcode but when I run the program on my device or simulators it won't display the custom font. I've already added the font file to the project and the font name to the "Fonts provided by application" array in info.plist. The font file is also added to "Copy Bundle Resources". I've tried all the solutions provided in some other similar questions but none of them worked so I'm not asking a repeated question.
Place this piece of code in your AppDelegate (didFinishLaunchingWithOptions:) to check that the custom fonts are available in the project:
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (#"%#: %#", fontFamily, fontNames);
}

Unable to set custom font for the UILabel in XCode

I am unable to set custom font for the UILabel in XCode.
This is what I've tried:
Download "JennaSue" font -- http://www.dafont.com/jenna-sue.font
Open "app-info.plist" under "Supporting Files" folder
Add new row in the list with key "Fonts provided by application"
In this array add "JennaSue.ttf"
Use it in the code like this:
self.someLabel.font = [UIFont fontWithName:#"JennaSue" size:14];
And nothing happens -- the default font is visible.
Why? What am I doing wrong? How can I fix it?
Be sure your font is in Bundle Resources. For some reason Xcode it is not importing custom font properly most of the time:
I've got the font working:
Example code: here
Go to your project's info.plist file,
Right click and select Add row,
Type Fonts provided by application in the new row,
Type in the desired font name as items for this key.
Drag and drop the font file into the project,
Apply it to the text you want:
someLabel.font = [UIFont fontWithName:#"JennaSue" size: 12.0];
I think you've missed step 5 up there.
Update: When doing step 5, remember to check the marks for copying the actual file into project directory:
Remember to clean your project by pressing "command+alt+shift+K" (IRRC!)
And then go to your
and then go to your project's Build Phases, and make sure you can see your .ttf file file among the resource files:
P.S. I'm not on my Mac at the moment, so I used screenshots from this tutorial. That's why I grayed out the unnecessary lines for your issue.
Check this code:
NSArray *names = [UIFont familyNames];
NSLog(#"Font FamilyNames : ");
for (NSString *name in names) {
NSLog(#"Font Family: %#",name);
NSArray *fontFaces = [UIFont fontNamesForFamilyName:name];
for (NSString *fname in fontFaces) {
NSLog(#" %#",fname);
}
}
self.someLabel.font = [UIFont fontWithName:#"use correct name" size:self.someLabel.font.pointSize];
and use the same name printed with NSLog.
After adding custom font to Xcode don't forget to add font to plist.
Project settings > Info > Custom IOS target properties
Add property Fonts provided by application and type your custom font(filename of font file).
Then you can use that code example
self.someLabel.font = [UIFont fontWithName:#"JennaSue" size:14];
Notice: make sure that you use right font name in code. To do that you should add this font to your Mac's font book, open font book, choose that font and check the right name font.
Hope this helps
self.someLabel.font = [UIFont fontWithName:#"JennaSue.ttf" size:self.someLabel.font.pointSize];
i used following code and it's done
UILabel * lblTest = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 100)];
lblTest.font = [UIFont fontWithName:#"JennaSue" size:25.0];
lblTest.text = #"Hello World";
[self.view addSubview:lblTest];
and if still not working looking at your system font book and search that font and That Font you have to use to get it work
also try this lblTest.font = [UIFont fontWithName:#"Jenna Sue" size:25.0];
The font could be corrupted or something. I tried the solutions provided by Gabriel.Massana & Neeku and it worked on some fonts. Try adding the font to your Mac. If it showed verification problem, most probably it's not gonna work in your app. Hope this helps
is the file visible in Compile Source now…if yes then you may have the font name wrong..sometimes the font names are different then their file name…try running a
for (NSString *familyName in [UIFont familyNames])
{
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName])
{
NSLog(#"%#", fontName);
}
}
this lines of code..you will be able to see the actual name of the font and use that name instead
Here is #Ritu's answer in Swift 2. Just put this in your AppDelegate, or in viewDidLoad method of your View Controller:
let names = UIFont.familyNames()
for name in names {
print("Font Family: \(name)")
let fontFaces = UIFont.fontNamesForFamilyName(name)
for fname in fontFaces {
print(" \(fname)")
}
}
You will have all your font names in console log, just copy and paste the one you need in your code.

How to integrate and use Font Awesome with Objective-C or Swift in an Xcode project?

So I am trying to use this font http://fortawesome.github.com/Font-Awesome/. I've added the font as a resource and put it in the plist file. Here's how I am using it:
[homeFeedButton.titleLabel setFont:[UIFont fontWithName:#"fontawesome" size:8]];
NSString *str = [NSString stringWithFormat:#"%C", #"\f030"];
However this doesn't work. Does anyone know how to use this font in an Xcode project?
In Swift:
Font Awesome Cheatsheet.
Tutorial on how to integrate the font called "Font Awesome" in your Xcode project.
Common Mistakes With Adding Custom Fonts to Your iOS App
let label = UILabel(frame: CGRectMake(0, 0, 100, 100))
label.font = UIFont(name: "FontAwesome", size: 40)
let myChar: UniChar = 0xF180
label.text = String(format: "%C", myChar)
self.view.addSubview(label)
BEST solution for FA with XCode:
Copy the icon you want from http://fontawesome.io/cheatsheet/ (I mean mark and copy the icon itself):
Then add a label to your storyboard, and on its properties:
RED - Choose FontAwesome as family
GREEN - Set the size you want
BLUE - Paste here what you copied in the 1st step. (dont worry about the question mark - in the view you should see it properly).
That's it.
If you need to change the icon in the code - you can paste 1st step inside your code too:
Not sure you ever got this working properly, but there's now a couple of nice projects on github:
https://github.com/alexdrone/ios-fontawesome - which gives you a category for NSString which offers basic help using FontAwesome.
and https://github.com/leberwurstsaft/FontAwesome-for-iOS which gives you a NSString category with fontAwesomeIconStringForIconIdentifier and also an UIImageView subclass: FAImageView
It is because your +[NSString stringWithFormat:] method contains the literal for a unichar, not an NSString, which is an object that uses %#, which is beside the point because literal'ing a literal is redundant.
You can just use the literal code string by affixing it with \u.
e.g. You can use \f030 in your iOS app using the following snippet.
[[[UILabel alloc] initWithFrame:CGRectZero] setText:#"\uf030"];
So, I can answer it for Swift & Objective C both. (using Swift 3.0 here)
Adding & configuring font-awesome file & Editing Storyboard
Just download font-awesome from here: fontawesome.io
and add .ttf to your project
Check the image below for more details...
Now the coding part
Swift 3.0
let iconUniChar: UniChar = 0xf113
textLabel.text = String(format: "%C", iconUniChar)
Objective C
_textLabel.text = [NSString stringWithFormat:#"%C", 0xf113];
And, if you are still facing the trouble here's the
entire source code
You can also use the library FontAwesome+iOS for iOS
Then you only need to use this code:
label.text = [NSString fontAwesomeIconStringForIconIdentifier:#"icon-github"];
// or:
label.text = [NSString fontAwesomeIconStringForEnum:FAIconGithub];
Below is the code to set image to a UIButton using FontAwesome
UIButton *btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenu.frame = CGRectMake(0, 0, 40, 32);
[btnMenu setTitle:#"\uf0c9" forState:UIControlStateNormal];
[btnMenu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnMenu.titleLabel setFont:[UIFont fontWithName:#"FontAwesome" size:16]];
NSLog your all font using following code and provide exact name.
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
NSLog(#"Family name: %#", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSLog(#" Font name: %#", [fontNames objectAtIndex:indFont]);
}
}
I was looking for the helper library for Swift3 and found this to be very simple and working solution https://github.com/Syndicode/FontAwesomeKitSwift3. It has animations that explain for novices like me how to include it and use quickly.
Here is an example for a cell's label text:
cell.textLabel.font = [UIFont fontWithName:#"FontAwesome" size:12];
cell.textLabel.text = #"\uf000";
Make sure you get the font name correct as suggested above.
not sure if this answer will be useful looking at the date you posted your question.... but to help someone maybe who is doing it at the moment,
[self.homefeedbutton setTitle:#"o" forState:UIControlStateNormal];
[self.homefeedbutton.titleLabel setFont:[UIFont fontWithName:#"fontawesome" size:30]];
setTitle:#"o" o is a name of the font, don't use the unichar, instead rename your font to something else preferable a letter, e.g if you check fontello.com , select icon, then click customize code tab, on top of you icon there is a small box, change it to any letter....
shout if its not clear enough , I will try to explain in a different way.
I have made a library in swift language with easy integration for UILabel, UIButton and UIBarButtonItem. Also supports Pods. Font Awesome Swift
This may help someone else... I had about 90% of Font Awesome icons working in my project, except a handful of them I couldn't get them to display correctly. They'd instead display a "..." icon instead.
After several hours of investigating unicode characters in Objective-C, I realised I was barking up the wrong tree! "..." isn't a FontAwesome icon, it's UIKit telling me that the frame of the UILabel is too small/font too large to display the text string! D'oh.
In case anyone is wondering, import the following in the .m file.
import "NSString+FontAwesome.h"
For using custom fonts make sure that the name of font which you have given in your plist and here,have the same real name.I mean the name of the font should be the same which in generally it have.Dont try to modify for the name.Check if the name of the font you are using is really the name of this font.
Thanks
Sanjay
FontAwesome font name comes in like so: FontAwesome
Try [UIFont fontWithName:#"FontAwesome" size:8]].
Watching this video tutorial might help: http://youtu.be/J1EHAS0icv0.

My custom font is not displaying

I'm trying to use a custom font in my iPhone app.
I created a key for it in my info.plist as follows:
<key>UIAppFonts</key>
<array>
<string>CloisterBlack.ttf</string>
</array>
and I'm accessing the font like this:
[UIFont fontWithName:#"CloisterBlack" size:64.0]
but the font is not displaying. What could be the reason?
Make sure you give the real font name in above code. The font file name and its “real font name” can be different, so just open the font in FontBook app and there you can see the real name of the font.
The "Real Font Name" is not always the one you see in Fontbook. The best way is to ask your device which fonts it sees and what the exact names are.
I use the uifont-name-grabber posted at:
http://forgecode.net/2010/08/uifont-name-grabber/
Just drop the fonts you want into the xcode project, add the file name to its plist, and run it on the device you are building for, it will email you a complete font list using the names that UIFont fontWithName: expects.
Add Font info in info.plist
Find name of font, by printing names of available fonts. You can do that with piece of code:
NSArray *fontFamiliesTemp = [UIFont familyNames];
for (int i = 0; i < [fontFamiliesTemp count]; i++)
{
NSString *fontFamily = [fontFamiliesTemp objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (#"%#: %#", fontFamily, fontNames);
}
Search for your font in printed results. After that you only need to use that font name.

Resources