3

I'm trying to use iOS FontAwesome to set an icon on a bar button, like this:

[self.barButton setTitle:[NSString fontAwesomeIconStringForEnum:FACamera]];

and also with this:

[self.barButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-camera"]];

No matter what identifier I use the result is the same:

enter image description here

What could be wrong?

Community
  • 1
  • 1
user623396
  • 1,537
  • 1
  • 17
  • 39
  • are you using the `FontAwesome.ttf` for your button as essential requirement? – holex Nov 24 '14 at 12:14
  • @holex, not sure where to set that... – user623396 Nov 24 '14 at 12:17
  • the `UIButton` has a `font` property – that can be a good start, but the original author has a detailed description to show how you could do it – if you had read the documentations... – holex Nov 24 '14 at 12:18
  • `UIBarButtonItem` doesn't have a font property, as far as I know. (no need to be caustic, btw) – user623396 Nov 24 '14 at 12:20
  • yes, it has not, you can change its font via the `appearance` protocol. – holex Nov 24 '14 at 12:22
  • Appearance protocol is not best practice in this case, IMHO. – user623396 Nov 24 '14 at 12:39
  • that is the official way to do it in Apple's definite opinion (IADO); and unfortunately the _IADO_ is much stronger argument here than the _IMHO_. :) – holex Nov 24 '14 at 12:42
  • Fortunately I found the solution I was looking for (see the answer below). Thanks anyway. – user623396 Nov 24 '14 at 12:43
  • probably you have spotted that you are doing the job via the `appearance` protocol exactly in your own answer, which you have been against to... just saying, you know. :) – holex Nov 24 '14 at 12:50
  • I take my words back. Couldn't have done it without your kind and precious help. Oh, wait, I've done it... ;) – user623396 Nov 24 '14 at 13:03
  • hehe, I love sarcarm, but nevermind, you have the solution which you have been looking for, and eventually we are here to help on each other; job has done. ;) – holex Nov 24 '14 at 13:13

5 Answers5

5

In case anyone else ever wonders:

[self.barButton setTitleTextAttributes:@{
                  NSFontAttributeName: [UIFont fontWithName:@"FontAwesome" size:24.0],
                  NSForegroundColorAttributeName: self.view.tintColor
                                         } forState:UIControlStateNormal]; 
[self.barButton setTitle:[NSString fontAwesomeIconStringForIconIdentifier:@"fa-camera"]];
user623396
  • 1,537
  • 1
  • 17
  • 39
2

In Swift:

    let filterButton = UIBarButtonItem(title: String.fontAwesomeIconWithName(FontAwesome.Filter), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("filterClicked"))
    filterButton.setTitleTextAttributes(NSDictionary(dictionary: [NSFontAttributeName:UIFont.fontAwesomeOfSize(25), NSForegroundColorAttributeName : UIColor.COLOR_BLUE_DARK]), forState: UIControlState.Normal)

    self.navigationItem.rightBarButtonItem = filterButton
Ibrahim Yildirim
  • 2,731
  • 2
  • 19
  • 31
1

With the FontAwesomeKit 2.2 API, you can do it like this:

[self.barButton setTitleTextAttributes:@{
    NSFontAttributeName: [FAKFontAwesome iconFontWithSize:20]
} forState:UIControlStateNormal];
[self.barButton setTitle:[FAKFontAwesome cameraIconWithSize:20].attributedString.string];

Or convert to UIImage like suggested in the FontAwesomeKit documentation:

FAKFontAwesome *cameraIcon = [FAKFontAwesome cameraIconWithSize:20];
UIImage *cameraImage = [cogIcon imageWithSize:CGSizeMake(20, 20)];
[self.barButton setImage:camera];
Greg Wang
  • 1,357
  • 1
  • 13
  • 17
0

Just in case you want to use easier solution without using attributed texts, look at my library. Cocoa pods are also supported. Font Awesome Swift

Patrik Vaberer
  • 646
  • 6
  • 15
0

In Swift 4, this worked for me

let button1 = UIBarButtonItem(title: "\u{f05a}", style: .plain, target: self, action: #selector(showCartInfoPopUp(_:)))
           
button1.setTitleTextAttributes([.font : UIFont(name: "Font Awesome 5 Pro", size: 24), .foregroundColor : UIColor.white], for: .normal)
            
self.navigationItem.rightBarButtonItem  = button1
Jasmine John
  • 873
  • 8
  • 12