根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
查看查看249 回复回复1 收藏收藏 分享淘帖 转播转播 分享分享 微信
查看: 249|回复: 1
收起左侧

根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift

[复制链接]
小开开 发表于 2016-6-30 11:11:41 | 显示全部楼层 |阅读模式
快来登录
获取优质的苹果资讯内容
收藏热门的iOS等技术干货
拷贝下载Swift Demo源代码
订阅梳理好了的知识点专辑

看到敏捷大拇指上讨论字体的这个帖子《怎样让工程里面的中文和英文使用不同的字体?》,我发此帖以兹参考Iconic库

Iconic will help you make icon fonts integration on iOS easy and effortless. Its main component is in charge of auto-generating strongly typed code in Swift, fully compatible with Objective-C, allowing the integration of vector icons as image or text in your apps.

根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift 1

根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift - 敏捷大拇指 - 根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift 1


You will interact with an auto-generated class under the name of {FontName}Icon.swift , which is a light abstraction of the Iconic.swift class. For more information, have a look athow to installandhow to use.

Note: This library hasn't yet been used in production. Consider it in beta!

根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift 2

根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift - 敏捷大拇指 - 根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift 2





1、Why Icon Fonts

Web developers have been using icon fonts for quite some time now. It's really, really great!

There are many advantages of using icon fonts on iOS:

  • Resolution independent: scale and tint without quality loss.
  • Automatically scaled for different screen densities.
  • Work with (way) less image files.
  • Improve visual consistency.
  • Platform agnostic.
  • Add better UI accessibility.
  • Simple to work with.


Think about the power of rendering vector icons, natively!

Great. Now, how do I create an icon font, you say?






2、Key Features

  • Very easy to install with CocoaPods.
  • Compatible with Swift and Objective-C.
  • Supports TTF and OTF font files.
  • Auto-generates enums and unicodes mapping, out of the font's PUA range .
  • NSAttributedString and UIImage outputs.
  • Fonts are registered dynamically, effortless. No need to import the file to your project.
  • UIKit extensions( UIBarButtonItem and UITabBarItem ).
  • Auto-generatedicon font html catalog.
  • iOS 8, and tvOS 9 or later.


Note: Some open sourced icon fonts don't include the names of each of their glyphs. This could result in a non-descriptive enum, which can make things less intuitive for you when using Iconic. If you create your own icon font, make sure to properly name each glyph.

Missing Features in Beta

  • Allow rectangular icon glyphs (right now, the lib assumes they're all square sized).
  • Provide OSS icon fonts with fully named glyphs for better usability.





3、Installation



3.1、Via CocoaPods

[Shell] 纯文本查看 复制代码
FONT_PATH='path_to_your_icon_font.ttf' pod install Iconic


When using the FONT_PATH environment variable, it will install Iconic with your icon font and auto-generate all files with its name.

You should then see a similar setup like this:

根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift 3

根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift - 敏捷大拇指 - 根据矢量字体文件生成实用的Swift本地渲染封装库 Iconic.swift 3


[Shell] 纯文本查看 复制代码
pod install Iconic


Will install Iconic with its default font,FontAwesome.



3.2、Under the hood

When installing Iconic, several things are happening under the hood:

  • After the Iconic repo is cloned, a custom version ofSwiftGen is downloaded along with its dependencies.
  • Before pods are installed, SwiftGen is compiled
  • Iconizer is ran, executing SwiftGen using a custom stencil for Iconic .
  • SwiftGen does its magic, detecting all unicodes from the PUA range of the provided font file, extracting their unicode values as well as their glyph names. All this data is then used for auto-generating a Switft class of name {FontName}Icon.swift ; a json file is also exported afterwards.
  • Once everything is exported, anHTML icon font catalogis also created.


There is a known bug where sometimes, calling pod install Iconic would not run correctly SwiftGen an retrieve all the icon unicode from a font. If this happens to you, make sur