YYText-swift,swift版的YYText,优化了yylabel和yytextview的部分扩展

致敬YYText的作者,YYText的Swift版本

Objecttive-C version of YYText添加链接描述

本项目写了YYTextView仿微信输入高度动态变化YYTextView限制输入字数YYLabel之Html富文本超链接检测及点击YYLabel之评论盖楼等四个示例。

扩展了YYLabel设置Html富文本方法

// MARK: - 添加html富文本配置扩展
extension YYLabel {
    /**
     *  设置html富文本,
     *  由于html转换属于耗时超时操作,异步后台处理
     *  假如是列表,转换完成后再刷新对应的Cell,否则会照成滚动卡顿
     */
    func yy_setHtmlAttributedString(text: String, font: UIFont, lineSpacing: CGFloat, color: UIColor = .black, linkColor: UIColor = .blue, alignment: NSTextAlignment? = nil, completion: ((NSMutableAttributedString?, Bool) -> Void)? = nil) {
        let defAttri = NSMutableAttributedString(string: text)
        defAttri.yy_color = color
        defAttri.yy_font = font
        defAttri.yy_lineSpacing = lineSpacing
        attributedText = defAttri
        
        DispatchQueue.global().async {
            var res:NSMutableAttributedString?
            if let data = text.data(using: .unicode) {
                do {
                    let attributed = try NSMutableAttributedString.init(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
                    attributed.yy_lineSpacing = lineSpacing
                    attributed.yy_color = color
                    attributed.yy_font = font
                    attributed.yy_alignment = alignment ?? .left
                    
                    attributed.enumerateAttributes(in: attributed.yy_rangeOfAll, options: .reverse) { keys, range, _ in
                        keys.forEach { i in
                            if i.key.rawValue == "NSLink" {
                                let highlight: YYTextHighlight = YYTextHighlight()
                                highlight.color = linkColor
                                highlight.yy_tapAction { _, _, _, _ in
                                    let link = "\(i.value)"
                                    UIApplication.shared.open(URL(string: link)!, options: [:], completionHandler: nil)
                                }

                                attributed.yy_set(color: linkColor, range: range)

                                attributed.yy_set(textHighlight: highlight, range: range)

                                attributed.yy_set(underlineStyle: NSUnderlineStyle.single, range: range)
                            }
                        }
                    }
                    res = attributed
                } catch _ {}
            }
            DispatchQueue.main.async {
                self.attributedText = res
                completion?(res, false)
            }
        }
    }
}

扩展了YYLoad,Runtime方法交换的实现方案

protocol YYLoad: Any {
    static func runOnce()
}

extension UIViewController:YYLoad {
    static func runOnce() {
        /// 这里写了两个示例给大伙看
        YYExchangeMethod(self, #selector(viewWillAppear(_:)), #selector(yy_viewWillAppear(_:)))
        
        YYExchangeMethod(self, #selector(touchesBegan(_:with:)), #selector(yy_touchesBegan(_:with:)))
    }

    @objc func yy_viewWillAppear(_ animated: Bool) {
        yy_viewWillAppear(animated)
    }
    
    @objc func yy_touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(false)
    }
}

扩展了YYTextView输入回调textViewWordCountChange

func textViewWordCountChange(_ textView: YYTextView, count: Int) {
        print("当前输入了\(count)个字")
}

github地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值