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
  • 22,200
  • 8
  • 54
  • 100
david
  • 3,310
  • 7
  • 36
  • 59

5 Answers5

7

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,580
  • 1
  • 17
  • 28
2

Follow these steps:

  1. Install fonts by double clicking on them.

  2. Drag and drop fonts in Xcode and make sure they are added to target. (check copy items if needed)

  3. In info.plist, set key Fonts provided by application and add font name for item(under key)

  4. Set font type custom in storyboard for your controls or programatically assign font to UILabel

Code:

   //In swift 3
   let font = UIFont(name: "iconmoon", 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:@"iconmoon" size:16];

Note: \u prefix is mandatory, Example for unicode e626, the label text should be \ue626 or \u{e626}

Saif
  • 2,678
  • 2
  • 22
  • 38
Gurjinder Singh
  • 9,221
  • 1
  • 66
  • 58
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
  • 342
  • 2
  • 10
0

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

Keyamoon
  • 1,785
  • 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
  • 4,383
  • 1
  • 34
  • 42