对于Xcode项目中,我们需要在plist中添加 Fonts provided by application 字典,并将字体(包含后缀名)写在字典中,如下图:
特别注意,必须检查下项目的build phase的项目资源中有没有刚刚添加的字体,没有的话需要添加上。如下图:
在UIlable中使用,作为字体使用
let label = UILabel(frame: CGRect(x: 50, y: 60, width: 250, height: 50))
label.font = UIFont.init(name: "iconFont", size: 20)
label.text = "\u{e645}" //OC语言:@"U0000e645" //注意:编码查看点击下载文件夹中的iocon.html查看
label.textColor = .gray
self.view.addSubview(label)
转换为image添加UIImageView中
//计算文本rect
let nscode = code as NSString
let rect = nscode.boundingRect(with:CGSize(width: 0.0, height: 0.0) , options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font : UIFont(name: "IconFont", size: fontSize)!], context: nil)
let size = rect.size
let label = UILabel(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
label.font = UIFont(name: "IconFont", size: fontSize)
label.textAlignment = .center
label.text = code
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
label.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
//
// LFIconFont.swift
// H56580E2E
//
// 使用字体图标
//
import UIKit
import CoreText
class LFIconFont: NSObject {
var code:String //标准解析格式 比如:"\u{a62b}"
var name:String //图标的名字.一般为中文,比如:设置
var descr:String! //图标作用和备注
var labelText:String! //可以直接赋值给UILabel,以系统字体大小为基准
var iconImage:UIImage!//生成一个一般的iconImge 如果需要其他大小的可以自己调整, 使用第二个初始化中代码段。
///初始化图标
/// - important : fontsize可通过外部font的size调整,而且必须设置UILabel的font,否则无法正常显示。比如:
/// - label.font = UIFont.init(name: "IconFont", size: UIFont.systemFontSize)
/// - parameter code: 标准解析格式,比如:"\u{a62b}"
/// - parameter name:图标的中文名
init(code:String, name:String) {
self.code = code
self.name = name
//为UILabel使用的做准备
let label = UILabel()
label.text = code
self.labelText = label.text
}
///初始化图标 ,图片可用
/// - important :图片大小由iconFont的fontSize自动计算决定。建议UIImageView大小参考打印值,这样的图片显示效果是最好的。
/// - parameter code: 标准解析格式,比如:"\u{a62b}"
/// - parameter name:图标的中文名
/// - parameter fontSize:图标(字体大小)
/// - parameter color:图标的颜色
convenience init(code:String, name:String, fontSize:CGFloat, color:UIColor){
self.init(code: code, name: name)
//计算文本rect
let nscode = code as NSString
let rect = nscode.boundingRect(with:CGSize(width: 0.0, height: 0.0) , options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font : UIFont(name: "IconFont", size: fontSize)!], context: nil)
let size = rect.size
print("建议图片大小:\(size)") //建议UIImageView大小参考打印值
let label = UILabel(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
label.font = UIFont(name: "IconFont", size: fontSize)
label.textAlignment = .center
label.textColor = color
label.text = code
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
label.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
self.iconImage = image
}
///直接生成该图标的UIImage 对象
/// - important :图片大小由iconFont的fontSize自动计算决定。建议UIImageView大小参考打印值,这样的图片显示效果是最好的。
/// - parameter fontSize:图标(字体大小)
/// - parameter color:图标的颜色
func iconFontImage(fontSize:CGFloat, color:UIColor) -> UIImage {
//计算iconFont的宽高
let nscode = code as NSString
let rect = nscode.boundingRect(with:CGSize(width: 0.0, height: 0.0) , options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: fontSize)], context: nil)
let size = rect.size
print("建议图片大小:\(size)") //建议UIImageView大小参考打印值
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
label.font = UIFont(name: "IconFont", size: fontSize) //??为什么用IconFont和字体文件名一致(首字母大小写无所谓),但是不能换为其他的,哪怕你的iconFont的名字为其他的。
label.textAlignment = .center
label.text = code
label.textColor = color
label.layer.render(in: UIGraphicsGetCurrentContext()!) //??深入了解layer
let image = UIGraphicsGetImageFromCurrentImageContext()
return image!
}
}
//MARK: - 扩展UILable
extension UILabel{
///直接通过UILabel加载iconfont
/// - parameter iconfont: 图标的编码标准格式如:"\u{a626}"
convenience init(iconfont code:String, frame: CGRect ,fontSize:CGFloat) {
self.init(frame: frame)
self.text = code
self.font = UIFont(name: "IconFont", size: fontSize)
self.textAlignment = .center
}
}
//MARK: - 扩展UIImage
extension UIImage {
///直接通过生成UIImage
/// - parameter code: 图标的标准编码 如:"\u{a626}"
/// - parameter fontSize: 图标的字体大小,建议设置为UIImageView的高度,这样最合适,效果最好。
/// - parameter color: 图标的颜色。
convenience init(iconfont code:String,fontSize:CGFloat, color:UIColor){
let iconImage = LFIconFont(code: code, name: "", fontSize: fontSize, color: color).iconImage
let iconData:Data = UIImagePNGRepresentation(iconImage!) ?? UIImageJPEGRepresentation(iconImage!, 1.0)!
self.init(data: iconData)!
}
}
开始使用
第一步:初始化LFIconFont
//定义图标(建议单独放在一个文件中,方便统一管理)
let icon_delete = LFIconFont(code: "\u{e645}", name: "删除")
let icon_pointer = LFIconFont(code: "\u{e504}", name: "大头针")
第二步:在label中使用
//在UILable中使用
let deletelabel = UILabel(frame: CGRect(x: 50, y: 100, width: 250, height: 50))
deletelabel.font = UIFont.init(name: "iconFont", size: 20) //在UILabel中使用是必须设置font,否则无法正常显示
deletelabel.text = "这是删除图标:" + icon_delete.labelText
deletelabel.textColor = .gray
self.view.addSubview(deletelabel)
转换为图标图片使用
//直接转化为Image图标使用
let deleteImage = icon_delete.iconFontImage(fontSize: 30, color: .green)
let imgView = UIImageView(frame: CGRect(x: 50, y: 200, width: deleteImage.size.width, height: deleteImage.size.height))
imgView.image = deleteImage
self.view.addSubview(imgView)
直接调用UIlabel初始化使用
//直接通过UILabel初始化
let iconLabel = UILabel(iconfont: icon_pointer.code, frame: CGRect(x: 50, y: 300, width: 50, height: 50), fontSize:18)
iconLabel.textColor = .blue
self.view.addSubview(iconLabel)
直接调用UIImge初始化
//直接初始化为UIImage
let iconImage = UIImage(iconfont: icon_pointer.code, fontSize: 50, color: .black) //fontSize一般和View高度差不多
let iconView = UIImageView(image: iconImage)
iconView.frame = CGRect(x: 50, y: 400, width: 50, height: 50)
self.view.addSubview(iconView)
LFIconFont-GitHub地址
参考https://blog.csdn.net/qq_14920635/article/details/78408761博主的文章