3

I got a file(please find attached file) which contain font, this font supposed to display icons according to code number. but i didn't succeed to display any icon with this font. here is my code:

    iconTest = UILabel();
    iconTest.frame = CGRectMake(0, 0, 100, 100);
    onTest.center = self.view.center;

    iconTest.font = UIFont(name: "icomoon", size:16.0);
    iconTest.text = "e906";

what's the correct way to display icons using font !

download font svg file

Jongware
  • 21,685
  • 8
  • 47
  • 95
david
  • 3,120
  • 6
  • 33
  • 55

5 Answers5

6

You have to set text property like:

iconTest.text = "\u{e906}";

Do not forget to register your custom font in your app.

Community
  • 1
  • 1
Martin Makarsky
  • 2,417
  • 1
  • 15
  • 25
1

Follow the following steps

1> Install fonts by double clicking on them.

2> Drag and drop fonts in Xcode and make sure they are added to target.

3> Set key - Fonts provided by application in info.plist and add font name for item(under key)

4> Set font type custom in storyboard for your controls

5> In swift 3

let font = UIFont(name: "icomoon", size: 15)
    btnSignout.setTitle("\u{e913}", for: UIControlState.normal) // set font icon on UIButton
    btnSignout.titleLabel!.font =  font

// In objectiveC
_lbl.text =  [NSString stringWithUTF8String:"\ue92e"]; // set font icon on UILabel
_lbl.font =  [UIFont fontWithName:@"icomoon" size:16];
Gurjinder Singh
  • 7,272
  • 1
  • 57
  • 51
0

I use icon font in some of my projects. I have create a project to use icon font easily. The README of this project is written in Chinese. But the demo project will show everything clearly. https://github.com/JohnWong/IconFont

Icon font is always used in UILabel by setting text. But I think creating a UIImage from font is more flexible. This is why I create this project. Method of register font by setting project is in answer provided by martin. Here is a way to register font by code: https://github.com/JohnWong/IconFont/blob/master/IconFont/TBCityIconFont.m#L16

John Wong
  • 345
  • 2
  • 9
0

Icomoon.swift: Use your Icomoon fonts with Swift - auto-generates type safe enums for each icon.

Keyamoon
  • 1,735
  • 17
  • 16
0

Despite it's not IcoMoon, this library is worth noting: https://github.com/0x73/SDevIconFonts

It integrates FontAwesome, Ionicons, Octicons and Iconic as Icon Fonts.

Assign them with code as easy as:

label.font = UIFont.iconFontOfSize(.FontAwesome, fontSize: 50.0)
label.text = String.fontAwesomeIconWithName(.Twitter)

As noted before, you have to register the font files in your app's Info.plist.

Anyway, regarding IcoMoon: I would use the above library as a starting point. Should be fairly easy (*) to add IcoMoon in a fork (or better as a pull request).

(*) technically spoken, adding a list of convenience shortcuts like .Twitter is quite an expensive task when I have a look at https://github.com/0x73/SDevIconFonts/blob/master/SDevIconFonts/Classes/FontAwesome.swift.

Frederik Winkelsdorf
  • 3,972
  • 1
  • 28
  • 40