SwiftyFontIcon
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
。
安装
SwiftyFontIcon 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'SwiftyFontIcon'
使用方法
-
在使用之前初始化 SwfIcon
- (推荐) 在您的 AppDelegate 中添加一行
import SwiftyFontIcon ............. SwfIcon.instance.loadAllAsync()
或者
- 在使用图标之前,以同步方式初始化它
import SwiftyFontIcon ............. SwfIcon.instance.loadAllSync() //Now you can start using it SwfIcon.instance.getUIImage(......)
您只需在应用程序启动后进行一次初始化
-
获取图标字符串或从图标生成图像
//Get NSAttributedString, the "gm-games" is the name of Google Material Design's "games" icon let iconicString = SwfIcon.instance.getNSMutableAttributedString("gm-games", fontSize: 10) //Set it to label, to button, or whatever place you like label.attributedText = iconicString //Get UIImage, "fa-heart" is the "Heart" icon from FontAwesome let iconicImage = SwfIcon.instance.getUIImage("fa-heart", iconSize: 100, iconColour: UIColor.blueColor(), imageSize: CGSizeMake(200, 200)) //Set it to UIImageView imgView.image = iconicImage // OR imgView.image = .icon(FAFontIcon.faHeart)
或者如果您想要更易读和简洁的方法
//FAFontIcon is a class that i generated contains predefined static FontIcon Objects imgView.image = .icon(FAFontIcon.faHeart, size: 100) // OR imgView.image = .icon(FontIcon(name: "fa-ad"), size: 100) // And for the color it changes based on image view tintColor imgView.tintColor = .blue
图标名称映射
内置图标来自
- Google 物料设计图标
- 所有Google物料图标都以"gm-"开头,后面跟着从上述链接中获取的实图标名。例如,"gm-account_balance_wallet"是"account_balance_wallet"图标的名字。
- Font Awesome
- 所有Font Awesome图标都以"fa-"开头,后面跟着Font Awesome图标的名称。例如,"fa-dashcube"是Font Awesome中"dashcube"图标的名称。
只需为SwfIcon提供您想渲染的图标名称,然后SwfIcon将为您处理其余部分。
添加您自己的图标字体
假设您有自己图标字体(相比于包含不同大小的资源图片,可以减小应用程序大小),并希望在您的iOS项目中显示,您只需要
//BEFORE you call SwfIcon init
//The font name prefix you want to use. For example, if you set it to "custom" and SwfIcon see an icon name start with "custom-", then it will know it's a custom font.
let customFontPrefix = "custom"
//Copy the ttf font file into your project and give SwfIcon the font file name (WITHOUT the ".ttf" extension)
let fontFileName = "custom_font" //Then SwfIcon will try to load the font from "custom_font.ttf" file
//The Font File Name, the fontName of your font. (The font name after you install the ttf into your system)
let fontName = "Custom"
//The icon name/value mapping dict ([FONT_NAME: FONT_UNICODE_VALUE])
let iconNameValueMappingDict = ["custom-1":"\u{f000}",...]
//Add custom font to SwfIcon
SwfIcon.instance.addCustomFont(prefix: customFontPrefix, fontFileName: fontFileName, fontName: fontName, fontIconMap: iconNameValueMappingDict)
//Then init SwfIcon
SwfIcon.instance.loadAllAsync() //Or Sync, depends on your needs
如果您需要自动化映射这些内容,并使用.icon(...)
语法创建强类型图标,您可以查阅这个工具 https://swiftyicongen.herokuapp.com
如何使用SwiftyIconGenerator生成您自己的自定义字体
- 打开https://swiftyicongen.herokuapp.com,您将看到一系列可供选择的预存在图标,您可以选择其中之一,或者您可以拖放您自己的SVG图标文件。
- 之后,为您图标命名一个类名,这应该是一个有效的Swift类名。
- 然后点击下载,您将收到一个包含三个文件的zip文件
config.json
{class_name}_icons.swift
{class_name}.ttf
- 将这些文件全部复制到您的项目目标中。
- 然后在内置字体之前调用这个方法
setupIconFont()
,在您的AppDelegate.swift
中。 - 现在您可以像这样使用您的图标在项目中:
imgView.image = .icon({class_name}.myIcon, size: 100)
要求
iOS 10或更高版本。Swift 4
作者
mohn93, [email protected]
灵感来源
许可协议
SwiftyFontIcon 在MIT许可下可用。有关更多信息,请参阅LICENSE文件。