Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 814
adit Avatar asked May 13 '12 06:05

adit


People also ask

How do I use swift types in Objective-C?

You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code.

How do I use Font Awesome icons with inline elements?

We designed Font Awesome for use with inline elements, and we recommend that you stick with a consistent element in your project. We recommend using <i> element with the Font Awesome CSS classes for the style class for the style of icon you want to use and the icon name class with the fa- prefix for the icon you want to use.

Why does Font Awesome Show icons instead of fonts?

Font Awesome uses that combination. If a page is cached, and loaded without the mouse over the window (i.e. hit the refresh button or load something in an iframe) then the page gets rendered before the font loads. Hovering over the page (body) will show some of the icons and hovering over the remaining icons will show those as well.

How does Font Awesome work with cached pages?

Font Awesome uses that combination. If a page is cached, and loaded without the mouse over the window (i.e. hit the refresh button or load something in an iframe) then the page gets rendered before the font loads. Hovering over the page (body) will show some of the icons and hovering over the remaining icons will show those as well.


4 Answers

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)
like image 166
King-Wizard Avatar answered Oct 23 '22 04:10

King-Wizard


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

like image 43
Danielle Avatar answered Oct 23 '22 02:10

Danielle


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

like image 14
ocodo Avatar answered Oct 23 '22 04:10

ocodo


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.

like image 8
CodaFi Avatar answered Oct 23 '22 04:10

CodaFi