以使用阿里巴巴矢量图为例
1、搜索需要的图标添加至购物车
E54D0DB9-F475-439F-9C1D-B38F936D9FAA.png2、将图片添加至项目
848E260C-9DB5-4C64-A31B-0D412CC8E0F3.png3、选择要添加的项目
CE9E9CC1-443A-4C6C-ABCA-4239338829DF.png4、添加项目成功后,会在我的项目里边看到所有添加的图标
5、接下来开始把图标用在我们的项目里边,首先需要下载字体文件,点击下载至本地即可下载,然后可把整个文件夹拖至项目
A48906CF-3D79-49A3-B0D4-73F78398C31A.png6、项目配置,打开Info.plist文件,增加一个新的Array类型的键,键名设置为Fonts provided by application,增加字体的文件名:“iconfont.ttf“。
屏幕快照 2019-06-11 下午5.25.09.png7、检查字体库是否添加成功
(1)通过代码输出查看是否返回iconfont
for family: String in UIFont.familyNames {
print("\(family)")
for names: String in UIFont.fontNames(forFamilyName: family) {
print("\(names)")
}
}
08F08083-770B-4E18-8441-7CB4A88C7BF4.png
(2)查看项目Build Phases->Copy Bundle Resources 是否包含.ttf文件
D8A729DF-5FB6-44FF-9E75-2464B1BCB312.png8、在项目中使用,代码如下
let button:UIButton = UIButton.init(type: .custom)
button.setTitle("\u{0000e608}", for: .normal)
button.setTitleColor(UIColor.red, for: .normal)
self.view.addSubview(button)
button.titleLabel?.font = UIFont.init(name: "iconfont", size: 18)
button?lf.view.height()-80-50-20, width: 50, height: 50)
注意:
(1)第5步的图片里边添加的图标下边有一串字符,转换成\uXXXXXXXX格式,可直接取后4位e608,然后前边拼接\u0000,最后用到的Unicode码就是\u0000e608
(2)需设置UIFont字体名字为iconfont
(3)直接写\u0000e608在swift里边会报错invalid escape sequence in literal,改成\u{0000e608}就可以了
网友评论