Objecttive-C version of YYText
本项目写了YYTextView仿微信输入高度动态变化、 YYTextView限制输入字数、YYLabel之Html富文本超链接检测及点击、YYLabel之评论盖楼等四个示例。
// 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)
}
}
}
}
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)
}
}
func textViewWordCountChange(_ textView: YYTextView, count: Int) {
print("当前输入了\(count)个字")
}
-
在 Podfile 中添加
pod 'YYText-swift'
。source 'https://github.com/CocoaPods/Specs.git' platform :ios, '13.0' use_frameworks! target 'MyApp' do # your other pod # ... pod 'YYText-swift' end
-
执行
pod install
或pod update
。 -
导入模块
import YYText_swift
如果帮助到你的话,点一下Star