Swift3.0下iconfont的使用

使用iconfont能有效减小应用的体积

一、OC语言下的使用

OC语言下面已经有很多友人写好了,这里提供几个参考地址:
在iOS开发中使用iconfont图标
iOS项目中iconfont使用指南

二、Swift3.0语言下的使用

在Swift语言下使用iconfont就比OC语言简单的多了,我们直接略过OC的模型
1、在viewDidLoad里定义一个label和imageView:

    let width = UIScreen.main.bounds.size.width
    
    let label = UILabel(frame: CGRect(x: width/2 - 150, y: 200, width: 300, height: 30))
    let iconFont = UIFont.init(name: "iconfont", size: 17)
    label.font = iconFont
    label.text = "这里是测试iconfont \u{e61e}"
    label.textAlignment = .center
    self.view.addSubview(label)
    
    let image: UIImageView = UIImageView(frame: CGRect(x: width/2 - 25, y: 100, width: 50, height: 50))
    let imageS: UIImage = HuzcIconFontMake.iconWithInfo(iconText: "\u{e61e}", iconSize: 50, iconColor: UIColor.red)
    image.image = imageS
    self.view.addSubview(image)

注释:上面这里有一个知识点、关于Unicode编码的查看问题,因为OC里可以直接用" @"\U0000e61e" " 的方式使用Unicode编码,,但是在Swift简洁了" "\u{e61e}" "。
2、建一个基于NSObject的类实现如下方法:

static var fontName: String = ""

class func registerFontWithURL(url: URL) {
    FileManager.default.fileExists(atPath: url.path)
    print(FileManager.default.fileExists(atPath: url.path))
    let fontDataProvider: CGDataProvider = CGDataProvider(url: url as CFURL)!
    let newFont: CGFont = CGFont.init(fontDataProvider)
    CTFontManagerRegisterGraphicsFont(newFont, nil)
}

class func fontWithSize(size: CGFloat) -> UIFont {
    var font = UIFont(name: HuzcIconFontMake.fontName, size: size)
    if font == nil {
        let fontFileURL = Bundle.main.url(forResource: HuzcIconFontMake.fontName, withExtension: "ttf")
        HuzcIconFontMake.registerFontWithURL(url: fontFileURL!)
        font = UIFont(name: HuzcIconFontMake.fontName, size: size)
        print("UIFont object should not be nil, check if the font file is added to the application bundle and you're using the correct font name")
    }
    return font!
}

class func set_FontName(fontName: String) {
    HuzcIconFontMake.fontName = (fontName == "" ? "" : "iconfont")
}

class func iconWithInfo(iconText: NSString, iconSize: CGFloat, iconColor: UIColor) -> UIImage {
    let scale: CGFloat = UIScreen.main.scale
    let realSize: CGFloat = iconSize * scale
    let font: UIFont = HuzcIconFontMake.fontWithSize(size: realSize)
    UIGraphicsBeginImageContext(CGSize(width: realSize, height: realSize))
    
    let context: CGContext = UIGraphicsGetCurrentContext()!
    
    let sel: Selector = #selector(iconText.draw(at:withAttributes:))
    if iconText.responds(to: sel) {
        iconText.draw(at: CGPoint.zero, withAttributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: iconColor])
    } else {
        context.setFillColor(iconColor.cgColor)
        iconText.draw(at: CGPoint(x: 0, y: 0), withAttributes: [NSFontAttributeName:font])
    }
    
    let image: UIImage = UIImage(cgImage: UIGraphicsGetImageFromCurrentImageContext()!.cgImage!, scale: scale, orientation: UIImageOrientation.up)
    UIGraphicsEndImageContext()
    
    return image
}

以上便是Swift使用iconfont的全部范文,最后附上个人的两个Demo
OCDemo & SwiftDemo
(如有不足、还请谅解!告知鄙人修改)

你可能感兴趣的:(Swift3.0下iconfont的使用)