Skip to content

Commit

Permalink
添加功能,可自定义资源下载
Browse files Browse the repository at this point in the history
  • Loading branch information
rztime committed Aug 27, 2024
1 parent 977be32 commit db9fa45
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 20 deletions.
7 changes: 1 addition & 6 deletions Example/RZRichTextView/HTML2AttrViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,8 @@ class HTML2AttrViewController: UIViewController {
}, disposebag: btn)

self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: btn)

var html = try? String.init(contentsOfFile: "/Users/rztime/Desktop/test.html")
html = """
<p style=\"margin:0.0px 0.0px 0.0px 0.0px;font-size:16.0px;\"><span style=\"color:#191919;font-size:16.0px;\">I yu</span></p><br/><p style=\"margin:0.0px 0.0px 0.0px 0.0px;font-size:16.0px;\"><span style=\"color:#191919;font-size:16.0px;\"><a href=\"https\">百度一下</a></span><span style=\"color:#191919;font-size:16.0px;\"></span></p><p style=\"margin:0.0px 0.0px 0.0px 0.0px;font-size:16.0px;\"><span style=\"color:#191919;font-size:16.0px;\">生活的</span></p>
"""
let html = (try? String.init(contentsOfFile: "/Users/rztime/Desktop/test.html"))
let t = "<body style=\"font-size:16px;color:#110000;\">\(html ?? "")</body>"
textView.html2Attributedstring(html: t)

}
}
27 changes: 27 additions & 0 deletions Example/RZRichTextView/HowToUseDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,39 @@ import Kingfisher

/// 使用时,直接将此代码复制到项目中,并完成相关FIXME的地方即可
public extension RZRichTextViewModel {
/// 如果有需要自定义实现资源下载,可以放开代码,并实现sync_imageBy、async_imageBy方法
static var configure: RZRichTextViewConfigure = {
/// 同步获取图片
RZRichTextViewConfigure.shared.sync_imageBy = { source in
print("sync source:\(source ?? "")")
let imgView = UIImageView()
imgView.kf.setImage(with: source?.qtoURL)
return imgView.image
}
/// 异步获取图片
RZRichTextViewConfigure.shared.async_imageBy = { source, complete in
print("async source:\(source ?? "")")
let comp = complete
let s = source
let imgView = UIImageView()
imgView.kf.setImage(with: source?.qtoURL) { result in
let image = try? result.get().image
comp?(s, image)
}
}
return RZRichTextViewConfigure.shared
}()
class func shared(edit: Bool = true) -> RZRichTextViewModel {
/// 自定义遮罩view 默认RZAttachmentInfoLayerView
// RZAttachmentOption.register(attachmentLayer: RZAttachmentInfoLayerView.self)

/// 如果有需要自定义实现资源下载,可以放开代码,并实现sync_imageBy、async_imageBy方法
// _ = RZRichTextViewModel.configure

let viewModel = RZRichTextViewModel.init()
viewModel.canEdit = edit
/// 链接颜色
viewModel.defaultLinkTypingAttributes = [.foregroundColor: UIColor.qhex(0x307bf6), .underlineColor: UIColor.qhex(0x307bf6), .underlineStyle: NSUnderlineStyle.styleSingle.rawValue]
/// 显示音频文件名字
// viewModel.showAudioName = false
/// 音频高度
Expand Down
2 changes: 1 addition & 1 deletion Example/RZRichTextView/LabelLoadHtmlViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LabelLoadHtmlViewController: UIViewController {
cell.reload = { [weak self] indexPath in
self?.tableView.reloadRows(at: [indexPath], with: .automatic)
}
cell.html = try? String.init(contentsOfFile: "/Users/rztime/Desktop/test1.html")
cell.html = try? String.init(contentsOfFile: "/Users/rztime/Desktop/test.html")
return cell
}
}
Expand Down
1 change: 0 additions & 1 deletion Example/RZRichTextView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class ViewController: UIViewController {
make.edges.equalToSuperview()
})
])

}

override func didReceiveMemoryWarning() {
Expand Down
17 changes: 13 additions & 4 deletions RZRichTextView/Classes/LabelRZRich.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,19 @@ public extension UILabel {
switch info.type {
case .image, .video:
let url = info.poster?.qtoURL ?? info.src?.qtoURL
UIImage.asyncImageBy(url?.absoluteString) {[weak info] image in
info?.image = image
info?.image = UILabel.creatAttachmentInfoView(info, width: max)
fix(attachment: at.0, range: at.1)
if let c = RZRichTextViewConfigure.shared.async_imageBy {
let complete: ((String?, UIImage?) -> Void)? = { [weak info] source, image in
info?.image = image
info?.image = UILabel.creatAttachmentInfoView(info, width: max)
fix(attachment: at.0, range: at.1)
}
c(url?.absoluteString, complete)
} else {
UIImage.asyncImageBy(url?.absoluteString) { [weak info] image in
info?.image = image
info?.image = UILabel.creatAttachmentInfoView(info, width: max)
fix(attachment: at.0, range: at.1)
}
}
case .audio:
info.image = UILabel.creatAttachmentInfoView(info, width: max)
Expand Down
14 changes: 11 additions & 3 deletions RZRichTextView/Classes/RZAttachmentInfoLayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ open class RZAttachmentInfoLayerView: UIView, RZAttachmentInfoLayerProtocol {
}
}
} else if let url = info.src {
self.imageView.kf.setImage(with: url.qtoURL, completionHandler: { [weak self] _ in
self?.updateImageViewSize()
})
if let c = RZRichTextViewConfigure.shared.async_imageBy {
let complete: ((String?, UIImage?) -> Void)? = { [weak self] source, image in
self?.imageView.image = image
self?.updateImageViewSize()
}
c(url, complete)
} else {
self.imageView.kf.setImage(with: url.qtoURL, completionHandler: { [weak self] _ in
self?.updateImageViewSize()
})
}
} else {
info.imagePublish.subscribe({ [weak self] value in
guard let self = self else { return }
Expand Down
26 changes: 21 additions & 5 deletions RZRichTextView/Classes/RZHtml.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,17 @@ public extension RZRichTextView {
case .image, .video:
let url = info.poster?.qtoURL ?? info.src?.qtoURL
if let url = url?.absoluteString {
UIImage.asyncImageBy(url) { [weak info] image in
info?.image = image
fix(attachment: at.0, range: at.1)
if let c = RZRichTextViewConfigure.shared.async_imageBy {
let complete: ((String?, UIImage?) -> Void)? = { [weak info] source, image in
info?.image = image
fix(attachment: at.0, range: at.1)
}
c(url, complete)
} else {
UIImage.asyncImageBy(url) { [weak info] image in
info?.image = image
fix(attachment: at.0, range: at.1)
}
}
}
case .audio:
Expand Down Expand Up @@ -400,10 +408,18 @@ public extension String {
at.rzattachmentInfo = att
switch att.type {
case .image:
att.image = UIImage.syncImageBy(att.src)
if let c = RZRichTextViewConfigure.shared.sync_imageBy {
att.image = c(att.src)
} else {
att.image = UIImage.syncImageBy(att.src)
}
case .video:
let src = ((att.poster.qisEmpty ? att.src : att.poster) ?? "")
att.image = UIImage.syncImageBy(src)
if let c = RZRichTextViewConfigure.shared.sync_imageBy {
att.image = c(att.src)
} else {
att.image = UIImage.syncImageBy(src)
}
case .audio:
break
}
Expand Down
16 changes: 16 additions & 0 deletions RZRichTextView/Classes/RZRichTextViewConfigure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// RZRichTextViewConfigure.swift
// RZRichTextView
//
// Created by rztime on 2024/8/23.
//

import UIKit

open class RZRichTextViewConfigure: NSObject {
public static var shared: RZRichTextViewConfigure = .init()
/// 同步获取图片
public var sync_imageBy: ((_ source: String?) -> UIImage?)?
/// 异步获取图片,complete里的source需要于图片对应
public var async_imageBy: ((_ source: String?, _ complete: ((_ source: String?, _ image: UIImage?) -> Void)?) -> Void)?
}

0 comments on commit db9fa45

Please sign in to comment.