700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > YYText-swift swift版的YYText 优化了yylabel和yytextview的部分扩展

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

时间:2024-04-28 19:11:18

相关推荐

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 = colordefAttri.yy_font = fontdefAttri.yy_lineSpacing = lineSpacingattributedText = defAttriDispatchQueue.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 = lineSpacingattributed.yy_color = colorattributed.yy_font = fontattributed.yy_alignment = alignment ?? .leftattributed.enumerateAttributes(in: attributed.yy_rangeOfAll, options: .reverse) {keys, range, _ inkeys.forEach {i inif i.key.rawValue == "NSLink" {let highlight: YYTextHighlight = YYTextHighlight()highlight.color = linkColorhighlight.yy_tapAction {_, _, _, _ inlet 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 = rescompletion?(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地址

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。