32

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?

King-Wizard
  • 15,628
  • 6
  • 82
  • 76
adit
  • 32,574
  • 72
  • 229
  • 373
  • Make sure that you are using ttf font and instal it first in your mac and then add to your project. – Charan May 13 '12 at 07:09
  • yup..I had it installed in my mac and it is ttf – adit May 13 '12 at 07:41
  • Ok, NSLog the font family and check whether the font name which you have installed in your mac is displaying or not – Charan May 13 '12 at 08:48
  • I can't seem to display the font in my mac.. can you try to download the font and give it a shot.. it's free – adit May 13 '12 at 16:57
  • I've not told to display in your mac, i have told you to NSLog the family of all the fonts and check whether this font is printing in the console or not – Charan May 13 '12 at 17:02
  • how do I NSLog the family of all the fonts? – adit May 13 '12 at 17:26
  • I think you need to check this http://developer.apple.com/library/ios/#documentation/uikit/reference/UIFont_Class/Reference/Reference.html check familyNames in classMethods – Charan May 14 '12 at 05:55
  • correct way is `[NSString stringWithFormat:@"%C", 0xf030];` – andilabs Aug 13 '15 at 21:17
  • Try this.. http://stackoverflow.com/a/34468957/3908884 – Meet Doshi Dec 26 '15 at 06:09

17 Answers17

18

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)
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
15

BEST solution for FA with XCode:

  1. Copy the icon you want from http://fontawesome.io/cheatsheet/ (I mean mark and copy the icon itself):

enter image description here

  1. Then add a label to your storyboard, and on its properties:

enter image description here

  • 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: enter image description here

Danielle
  • 860
  • 10
  • 13
14

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

ocodo
  • 29,401
  • 18
  • 105
  • 117
8

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.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
7

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

Jais
  • 611
  • 7
  • 4
  • Feels like this is the right track but doesn't work for me, does it work for you? – Ege Akpinar Dec 26 '12 at 00:31
  • This worked for me as i wanted to use an icon of the Flat UI Kit Icon Font (http://designmodo.github.io/Flat-UI/). I had copied the CSS value for the icon and just prepended the `u` as said above. It worked! :-) Tried so often without luck, but this finally worked out! – mavilein Sep 20 '13 at 07:16
  • I wonder if there is a way to enter this from storyboard UI. I tried to but only showed up as text. Usually we have designer work on putting icons so it would be nice to be able to do without touching the code – Shinya Koizumi Aug 21 '15 at 18:54
  • @powerfade917, you can use the storyboard - just copy and paste the icon (not the code or unicode) from the cheatsheet (http://fortawesome.github.io/Font-Awesome/cheatsheet/) into a label of something in your storyboard. Don't forget to set the font to FontAwesome. – jt_uk Sep 19 '15 at 22:50
6

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...

follow the sequence given in the image

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

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
5

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];
Néstor
  • 1,317
  • 13
  • 24
3

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]];
Sudheer Kumar Palchuri
  • 2,919
  • 1
  • 28
  • 38
2

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]);
        }
    }
McDowell
  • 107,573
  • 31
  • 204
  • 267
Gaurav
  • 8,227
  • 4
  • 34
  • 55
  • While this is an old answer, it still helped me solve why my custom font file wasn't loading. Turns out the font family name changed as opposed to the name I have been using in my app for a long time. Go figure. Thanks for this. – ChrisOSX Apr 20 '16 at 10:27
1

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.

Anton
  • 927
  • 10
  • 15
0

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.

Kurt Madel
  • 66
  • 3
0

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.

0

I have made a library in swift language with easy integration for UILabel, UIButton and UIBarButtonItem. Also supports Pods. Font Awesome Swift

Patrik Vaberer
  • 646
  • 6
  • 15
0

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.

jt_uk
  • 1,362
  • 1
  • 15
  • 17
0

In case anyone is wondering, import the following in the .m file.

import "NSString+FontAwesome.h"
Gangani Roshan
  • 583
  • 1
  • 8
  • 25
-1

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

Sabby
  • 2,586
  • 2
  • 27
  • 41
-1

FontAwesome font name comes in like so: FontAwesome

Try [UIFont fontWithName:@"FontAwesome" size:8]].

Watching this video tutorial might help: http://youtu.be/J1EHAS0icv0.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62