iOS开发使用iconfont,封装返回UILabel、UIImage、UIImageView

为啥使用iconfont 理由一堆,反正现在就让我用了。 纯色ICon,都用iconfont。
用的多,所以要封装一下。 iconfont可以用于UILabel显示,也可以是UIImage。 我这里封装了一个返回UILabel和UIImageView的,连同带点击事件。
刚开始网上找找有没有封装好的方法,一直都不太好用,干脆自己搞一个吧。
直接上代码
  1. 先获取所有iconfont名称,后面方便直接用
ini 复制代码
public enum IconFontName: String {
    case downLoad = "\u{e60f}"
    case selectRight = "\u{e611}"
    case rightArrow = "\u{e612}"
    case closeIcon = "\u{e614}"
    case listIcon = "\u{e616}" 
    case avater = "\u{e617}"
    case backIcon = "\u{e618}"
    case feedback = "\u{e619}"
    case wrongIcon = "\u{e61b}" 
    case editIcon = "\u{e61c}"
    case warning = "\u{e610}"  
    case score = "\u{e60e}"
    case time = "\u{e60c}"
    case imageIcon = "\u{e60d}"
}

2.返回UILabel,带点击事件

ini 复制代码
class ExIconFont {

    class func iconFontToLabel(frame: CGRect = .zero, fontSize: CGFloat, fontName: String, iconName: String = "", action:(()->Void)?) -> UILabel 
        let lab = UILabel(frame: frame)
        lab.font = R.font.iconfont(size: fontSize)
        lab.text = fontName
        lab.isUserInteractionEnabled = true
        let tap = ClosureTapGestureRecognizer()
        lab.addGestureRecognizer(tap)
        tap.addAction { tap in
            guard let action = action else {
                return
            }
            action()
        }
        return lab
    }

3.返回UILabel有时候不行,那就在加封装返回一个UIImage 或者 UIImageView。 我这里直接返回了UIImageView, 方便使用。

swift 复制代码
class func iconfontToImage(iconText: String, fontSize: CGFloat, fontColor: UIColor, action:(()->Void)?) -> UIImageView {
        // 这里我使用的R.Swift资源管理, 你可以直接用:UIFont(name: "你的iconfont名称", size: fontSize)
        let font = R.font.iconfont(size: fontSize)
        let attributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: fontColor]
        let attributedString = NSAttributedString(string: iconText, attributes: attributes as [NSAttributedString.Key : Any])

        UIGraphicsBeginImageContextWithOptions(CGSize(width: fontSize, height: fontSize), false, 0)
        attributedString.draw(at: CGPoint(x: 0, y: 0))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        // 如果不想用UIIimageView , 这里直接返回image即可。 return image
        
        let imageV = UIImageView(image: image)
        imageV.isUserInteractionEnabled = true
        let tap = ClosureTapGestureRecognizer()
        imageV.addGestureRecognizer(tap)
        tap.addAction { tap in
            guard let action = action else {
                return
            }
            action()
        }
        return imageV
    }

3.自定义一个点击事件封装

swift 复制代码
/// 自定义tapgesture 闭包方式回调
class ClosureTapGestureRecognizer: UITapGestureRecognizer {
    private var action: ((UITapGestureRecognizer) -> Void)?

    func addAction(_ action: @escaping ((UITapGestureRecognizer) -> Void)) {
        self.action = action
    }

    @objc func handleAction(sender: UITapGestureRecognizer) {
        guard let action = action else { return }

        action(sender)
    }

   
    override init(target: Any?, action: Selector?) {
        super.init(target: target, action: action)

        self.addTarget(self, action: #selector(handleAction(sender:)))
    }
}

4.使用方法

php 复制代码
        // 返回label
        let iconLabel = ExIconFont.iconFontLabel(fontSize: 24, fontName: IconFontName.avater.rawValue) {
           self.navigationController?.pushViewController(PersonViewController(), animated: true)
        }
        // 返回UIImageView
        let iconFontImageV = ExIconFont.iconfontToImage(iconText: IconFontName.avater.rawValue, fontSize: 24, fontColor: .black) {
        // 点击事件
           self.navigationController?.pushViewController(PersonViewController(), animated: true)
        }

        // 直接赋值
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: iconFontImageV)

完活~

相关推荐
恋猫de小郭17 小时前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
点金石游戏出海1 天前
每周资讯 | Krafton斥资750亿日元收购日本动画公司ADK;《崩坏:星穹铁道》新版本首日登顶iOS畅销榜
游戏·ios·业界资讯·apple·崩坏星穹铁道
旷世奇才李先生1 天前
Swift 安装使用教程
开发语言·ios·swift
90后的晨仔1 天前
Xcode16报错: SDK does not contain 'libarclite' at the path '/Applicati
ios
finger244801 天前
谈一谈iOS线程管理
ios·objective-c
Digitally1 天前
如何将大型视频文件从 iPhone 传输到 PC
ios·iphone
梅名智1 天前
IOS 蓝牙连接
macos·ios·cocoa
美狐美颜sdk1 天前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭2 天前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
泓博2 天前
Objective-c把字符解析成字典
开发语言·ios·objective-c