How to integrate and use Font Awesome with Objecti

2019-01-21 05:13发布

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?

17条回答
时光不老,我们不散
2楼-- · 2019-01-21 05:25

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.

查看更多
甜甜的少女心
3楼-- · 2019-01-21 05:25

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.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-21 05:27

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

查看更多
闹够了就滚
5楼-- · 2019-01-21 05:27

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]);
        }
    }
查看更多
干净又极端
6楼-- · 2019-01-21 05:27

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

import "NSString+FontAwesome.h"
查看更多
【Aperson】
7楼-- · 2019-01-21 05:28

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.

查看更多
登录 后发表回答