Use of Swift or objective-c Iconfont fonts

Source: Internet
Author: User

iOS can use a single icon (svg,png) directly. You can also introduce font applications directly:

1: Add the Font file (. ttf) you downloaded from the Iconfont platform to the project;

Open the Info.plist file, add a new array type key, set the key name to Uiappfonts (Fonts provided by application), increase the font filename: "Iconfont.ttf"

2. Objective-c Use Iconfont font
UILabel * label =*iconfont = [Uifont fontwithname:@ "uxiconfont"  =@ "\u00003439 \u000035ad \u000035ae \u000035af \u000035eb \u000035ec"  ; [Self.view Addsubview:label];


In general, Iconfont is used for uiimage classes. You can write a Uiimage+iconfont classification to implement icon as a function of the image.
+ (uiimage*) Imagewithicon: (nsstring*) Iconcode Infont: (nsstring*) fontname size: (nsuinteger) Size color: (uicolor*) Color {cgsize imageSize=cgsizemake (size, size); Uigraphicsbeginimagecontextwithoptions (ImageSize, NO, [[UIScreen mainscreen] scale]); UILabel*label = [[UILabel alloc] Initwithframe:cgrectmake (0,0, size, size)]; Label.font=[Uifont fontwithname:fontname size:size]; Label.text=Iconcode; if(color) {Label.textcolor=color; } [Label.layer Renderincontext:uigraphicsgetcurrentcontext ()]; UIImage*retimage =Uigraphicsgetimagefromcurrentimagecontext (); returnRetimage;}

Here are two places to note:

    • The Uifont is created using the font name, not the file name;
    • The literal value is 8-bit Unicode character, we can open demo.html to find each icon corresponding to the HTML entity Unicode code, such as: "Store" corresponding to the HTML entity Unicode code: 0x3439 after conversion: \u00003439 is to replace 0x with \u middle with 0 filled with a full length of 8 characters
3. Swift 3.0 uses Iconfont fonts
Let label =  00, width:info.textsize!, height:info.textsize!  " Iconfont "  -  == \u{e60a} self.view.addSubview (label)    
In general, Iconfont is used for uiimage classes. You can write a taxonomy to inherit UIImage to implement icon as a picture function.
OpenclassFunc Iconbwithinfo (_ Info:iconinfo)uiimage{let Imagesize:cgsize= Cgsize (width:info.textsize!, height:info.textsize!) uigraphicsbeginimagecontextwithoptions (ImageSize,false, UIScreen.main.scale) let label= UILabel (Frame:cgrect (x:0Y:0, width:info.textsize!, height:info.textsize!)) Label.font= Uifont (Name:"Iconfont", Size: -) Label.textalignment=. Center Label.text=Info.textif(Info.textcolor! =Nil) {Label.textcolor=Info.textcolor} label.layer.render (inch: Uigraphicsgetcurrentcontext ()!) Let Image:uiimage= Uigraphicsgetimagefromcurrentimagecontext ()!returnImage}

Note: Swift uses Unicode encoding different from OC, Swift is \u{e60a} and OC is the \u0000e60a point to note.

Finally, add the Alibaba font vector icon Library

Use of Swift or objective-c Iconfont fonts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.