V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
EyreFree
V2EX  ›  iDev

[iOS 开源库] EFIconFont - 一个 Swift 实现的 IconFont 封装,帮助你便捷地在你的工程中使用 IconFont

  •  
  •   EyreFree ·
    EyreFree · 2019-03-25 11:31:51 +08:00 · 4319 次点击
    这是一个创建于 1831 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个用 Swift 实现的普通的 IconFont 封装,帮助你更便捷地在你的工程中使用 IconFont,同时集成了一系列可免费使用的第三方图标库。

    English Introduction

    示例

    1. 利用 git clone 命令下载本仓库;
    2. 利用 cd 命令切换到 Example 目录下,执行 pod install 命令;
    3. 随后打开 EFIconFont.xcworkspace 编译即可。

    或执行以下命令:

    git clone [email protected]:EFPrefix/EFIconFont.git; cd EFIconFont/Example; pod install; open EFIconFont.xcworkspace
    

    需求

    • iOS 8.0+
    • Swift 4.2+

    安装

    EFIconFont 可以通过 CocoaPods 进行获取。只需要在你的 Podfile 中添加如下代码就能实现引入,默认只包含 Core 部分,不含字体包:

    pod 'EFIconFont'
    

    可以通过 subspecs 方式引入本库已集成的图标库资源,如下示例引用了 AntDesign 和 FontAwesome 资源:

    pod 'EFIconFont', :subspecs => ['AntDesign', 'FontAwesome']
    

    也可以通过 Complete 引入本库已集成的所有图标库资源,示例:

    pod 'EFIconFont', :subspecs => ['Complete']
    

    然后,执行如下命令即可:

    pod install
    

    使用

    1. 核心

    实现 EFIconFontProtocol 协议的对象,能够将自身转换为 NSAttributedStringUIImage,该协议内容如下:

    public protocol EFIconFontProtocol {
    
        // `name` is not necessarily equal to .ttf file name
        var name: String { get }
    
        // `path` is path of .ttf file
        var path: String { get }
    
        // `attributes` is style of icon
        var attributes: [NSAttributedString.Key : Any] { set get }
    
        // `unicode` is unique identifier of particular icon
        var unicode: String { get }
    }
    
    • name:字体名,与 .ttf 文件名并不一定相等,可通过 BirdFont 等字体文件处理工具查看其 Name 属性取得;
    • path:.ttf 文件路径,一般通过形如 Bundle.main.path(forResource: name, ofType: "ttf") 的方式获取(若文件名和 name 相同,则无须实现该属性,使用默认实现即可);
    • attributes: 某 icon 的样式(若无特殊需求,使用默认实现即可);
    • unicode:某符号的 unicode。

    实现该协议的对象,可通过调用下列方法进行转换输出为字符串和图片,可改变前景色和大小:

    // MARK:- String
    func attributedString(size fontSize: CGFloat, attributes: [NSAttributedString.Key : Any]?) -> NSAttributedString?
    func attributedString(size fontSize: CGFloat, foregroundColor: UIColor? = nil, backgroundColor: UIColor? = nil) -> NSAttributedString?
    
    // MARK:- Image
    func image(size fontSize: CGFloat, attributes: [NSAttributedString.Key : Any]?) -> UIImage?
    func image(size fontSize: CGFloat, foregroundColor: UIColor? = nil, backgroundColor: UIColor? = nil) -> UIImage?
    func image(size imageSize: CGSize, attributes: [NSAttributedString.Key : Any]?) -> UIImage?
    func image(size imageSize: CGSize, foregroundColor: UIColor? = nil, backgroundColor: UIColor? = nil) -> UIImage?
    

    2. 自带图标库

    本库已集成了 AntDesign、FontAwesome 等免费图标库资源,需要使用的同学引入即可,如下所示,会得到一个 EFIconFontProtocol 类型的返回值:

    EFIconFontAntDesign.addteam
    

    可通过遵循 EFIconFontProtocol 协议的对象获取 NSAttributedStringUIImage

    EFIconFontAntDesign.addteam.attributedString(size: 24)
    EFIconFontFontAwesomeBrands.adobe.attributedString(size: 32, foregroundColor: UIColor.white, backgroundColor: UIColor.green)
    EFIconFontFontAwesomeRegular.addressBook.image(size: 24, foregroundColor: UIColor.red)
    EFIconFontFontAwesomeSolid.alignLeft.image(size: CGSize(width: 36, height: 48), foregroundColor: UIColor.white)
    

    可通过如下方式获取某个图标库的全部项目,他会返回 [String : EFIconFontProtocol] 类型的 Dictionary:

    EFIconFont.antDesign.dictionary
    

    备注:虽为免费图标库,但还请自行确保您的使用方式遵循字库原始作者的使用协议规范。

    3. 自定义图标库

    (1) 字体文件引入

    将我们通过各种方式获取的图标库的 .ttf 文件拖入 Xcode 工程中,并确保 Build Phases 中的 Copy Bundle Resources 列表中包含这个字体文件(默认拖入工程就会被包含在内)。

    另外,此文件会在运行时按需加载,无需添加到 Info.plist 文件中的 Fonts provided by application 项内。

    (2) 实现 EFIconFontCaseIterableProtocol

    可通过实现 EFIconFontCaseIterableProtocol 协议实现图标库的封装,本项目中 Example 以 GitHub 所有的 Octicons 为例 演示 了自定义方式:

    import EFIconFont
    
    public extension EFIconFont {
        public static let octicons = EFIconFontOcticons.self
    }
    
    extension EFIconFontOcticons: EFIconFontCaseIterableProtocol {
        public static var name: String {
            return "octicons"
        }
        public var unicode: String {
            return self.rawValue
        }
    }
    
    public enum EFIconFontOcticons: String {
        case thumbsup = "\u{e6d7}"
        case unverified = "\u{e6d6}"
        case unfold = "\u{e6d5}"
        case verified = "\u{e6d4}"
        // ...
    }
    

    有人要问这个巨长无比的枚举是怎么手打出来的?当然是代码生成的了...先把 .svg 图片上传到 iconfont.cn,然后看 这里

    (3) 调用

    同上自带图标库的使用。

    EFIconFontOcticons.thumbsup
    

    (4) 注意事项

    本项目 Example 中的 Octicons 图标库为 GitHub 所有,此处仅为演示,请勿用于任何违反其所有者所定规范的场合:GitHub Logos and Usage

    4. 其它

    一些 IconFont 资源站点素材的爬取以及代码生成方式:

    作者

    EyreFree, [email protected]

    协议

    EFIconFont 基于 MIT 协议进行分发和使用,更多信息参见 协议文件


    #iOS 开源库# EFIconFont 0.4.1 发布啦,🎉

    EFIconFont 是一个用 Swift 实现的普通的 IconFont 封装,帮助你更便捷地在你的工程中使用 IconFont,同时集成了一系列可免费使用的第三方图标库,如 ElusiveIcons、FontAwesome、以及 Google 家的 MaterialIcons 等,详情可参考 Demo 工程及文档。

    欢迎大佬们使用、反馈,好用的话别忘了点个小星星,💥...仓库地址:https://github.com/EFPrefix/EFIconFont

    4 条回复    2021-06-28 15:46:25 +08:00
    designer
        1
    designer  
       2019-03-25 11:35:00 +08:00
    这个项目不错哦,支持下。
    顺便广告下自己画的一套矢量像素图标: http://chuangzaoshi.com/icon/
    DamonYu
        2
    DamonYu  
       2019-04-02 18:22:37 +08:00 via iPhone
    DA 佬你来啦
    EyreFree
        3
    EyreFree  
    OP
       2019-04-02 18:26:05 +08:00
    @DamonYu 大佬好,[扑通]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3220 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:52 · PVG 18:52 · LAX 03:52 · JFK 06:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.