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

后端 未结 17 1342
不思量自难忘°
不思量自难忘° 2020-12-12 18:07

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:

相关标签:
17条回答
  • 2020-12-12 18:50

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

    0 讨论(0)
  • 2020-12-12 18:50

    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.

    0 讨论(0)
  • 2020-12-12 18:52

    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];
    
    0 讨论(0)
  • 2020-12-12 18:53

    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]];
    
    0 讨论(0)
  • 2020-12-12 18:55

    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

    0 讨论(0)
  • 2020-12-12 18:58

    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]);
            }
        }
    
    0 讨论(0)
提交回复
热议问题