Skip to content

Commit

Permalink
修复(某些情况下,图片宽高在textView中显示错误)(超链接文字在textView中的默认属性)
Browse files Browse the repository at this point in the history
  • Loading branch information
rztime committed Aug 19, 2024
1 parent 0031805 commit 19d2cb8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
42 changes: 25 additions & 17 deletions Example/RZRichTextView/HTML2AttrViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,35 @@ import RZRichTextView

class HTML2AttrViewController: UIViewController {

lazy var textView = RZRichTextView.init(frame: .init(x: 15, y: 100, width: qscreenwidth - 30, height: 300), viewModel: .shared(edit: true))
lazy var textView = RZRichTextView.init(frame: .init(x: 16, y: 100, width: qscreenwidth - 32, height: 80), viewModel: .shared(edit: true))
// .qisScrollEnabled(false)
.qbackgroundColor(.qhex(0xf5f5f5))
.qplaceholder("请输入内容")
// .qlinkTextAttributes([.foregroundColor: UIColor.red])

override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.qbody([
textView.qmakeConstraints({ make in
// make.left.right.equalToSuperview().inset(15)
make.left.equalToSuperview().inset(15)
make.top.equalToSuperview().inset(100)
make.bottom.equalToSuperview().inset(qbottomSafeHeight)
make.width.equalTo(qscreenwidth - 30)
}),
])

NotificationCenter.qaddKeyboardObserver(target: self, object: nil) { [weak self] keyboardInfo in
let y = keyboardInfo.frameEnd.height
let ishidden = keyboardInfo.isHidden
self?.textView.snp.updateConstraints({ make in
make.bottom.equalToSuperview().inset(ishidden ? qbottomSafeHeight : y)
UIScrollView.qbody([
textView.qmakeConstraints({ make in
make.left.equalToSuperview().inset(16)
make.top.equalToSuperview()
make.height.equalTo(300)
make.width.equalTo(qscreenwidth - 32)
}),
]).qmakeConstraints({ make in
make.edges.equalToSuperview()
})
}
])
textView.textContainerInset = .init(top: 8, left: 0, bottom: 20, right: 0)
// NotificationCenter.qaddKeyboardObserver(target: self, object: nil) { [weak self] keyboardInfo in
// let y = keyboardInfo.frameEnd.height
// let ishidden = keyboardInfo.isHidden
// self?.textView.snp.updateConstraints({ make in
// make.bottom.equalToSuperview().inset(ishidden ? qbottomSafeHeight : y)
// })
// }
let btn = UIButton.init(type: .custom)
.qtitle("转html")
.qtitleColor(.red)
Expand Down Expand Up @@ -65,7 +70,10 @@ class HTML2AttrViewController: UIViewController {

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

let html = try? String.init(contentsOfFile: "/Users/rztime/Desktop/test.html")
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 t = "<body style=\"font-size:16px;color:#110000;\">\(html ?? "")</body>"
textView.html2Attributedstring(html: t)

Expand Down
3 changes: 1 addition & 2 deletions Example/RZRichTextView/HowToUseDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public extension RZRichTextViewModel {
viewModel.uploadAttachmentsComplete.subscribe({ value in
print("上传是否完成:\(value)")
}, disposebag: viewModel)

/// 有新的附件插入时,需在附件的infoLayer上,添加自定义的视图,用于显示图片、视频、音频,以及交互
viewModel.reloadAttachmentInfoIfNeed = { [weak viewModel] info in
viewModel.reloadAttachmentInfoIfNeed = { [weak viewModel] info in
/// 绑定操作,用于重传,删除、预览等功能
info.operation.subscribe({ [weak viewModel] value in
switch value {
Expand Down
2 changes: 1 addition & 1 deletion RZRichTextView/Classes/RZAttachmentInfoLayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ open class RZAttachmentInfoLayerView: UIView, RZAttachmentInfoLayerProtocol {

/// 音频相关view
var nameLabel = UILabel().qfont(.systemFont(ofSize: 12))
var audioPlayBtn = UIButton.init(type: .custom).qimage(RZRichImage.imageWith("audio"))
var audioPlayBtn = UIButton.init(type: .custom).qimage(RZRichImage.imageWith("audio")).qisUserInteractionEnabled(false)

/// 删除按钮
var deleteBtn: UIButton = .init(type: .custom).qimage(RZRichImage.imageWith("delete"))
Expand Down
15 changes: 13 additions & 2 deletions RZRichTextView/Classes/RZHtml.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ public extension RZRichTextView {
}
}
}
/// 修改链接字体
attr.enumerateAttribute(.link, in: .init(location: 0, length: attr.length)) { link, range, _ in
if link != nil {
attr.removeAttribute(.foregroundColor, range: range)
attr.removeAttribute(.strokeColor, range: range)
attr.removeAttribute(.strokeWidth, range: range)
attr.removeAttribute(.underlineStyle, range: range)
attr.removeAttribute(.underlineColor, range: range)
attr.addAttributes(self.linkTextAttributes, range: range)
}
}
self.textStorage.setAttributedString(attr)
self.selectedRange = .init(location: self.textStorage.length, length: 0)
}
Expand Down Expand Up @@ -427,8 +438,8 @@ public extension String {
(">","&gt;"),
(" ","&nbsp;"),
("©","&copy;"),
("®","&trade;"),
("","&nbsp;"),
("®","&reg;"),
("","&trade;"),
]
var temp = self
symbols.forEach { (v1, v2) in
Expand Down
11 changes: 8 additions & 3 deletions RZRichTextView/Classes/RZRichTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ open class RZRichTextView: UITextView {
accessoryView.clicked = { [weak self] item in
self?.didClickedAccessoryItem(item)
}

self.linkTextAttributes = self.viewModel.defaultLinkTypingAttributes
self.typingAttributes = self.viewModel.defaultTypingAttributes
self.lastTexttypingAttributes = self.typingAttributes
NotificationCenter.qaddKeyboardObserver(target: self, object: nil) { [weak self] keyboardInfo in
Expand Down Expand Up @@ -117,7 +117,8 @@ open class RZRichTextView: UITextView {
if textView.textStorage.length == range.location {
textView.typingAttributes = self.lastTexttypingAttributes
}
if self.viewModel.removeLinkWhenInputText {
if self.viewModel.removeLinkWhenInputText, let _ = textView.typingAttributes[.link] {
textView.typingAttributes = self.lastTexttypingAttributes
textView.typingAttributes[.link] = nil
}
if range.length == 0 && range.location == 0 && replaceText == "", let p = textView.typingAttributes[.paragraphStyle] as? NSParagraphStyle, p.isol || p.isul {
Expand Down Expand Up @@ -384,7 +385,11 @@ public extension RZRichTextView {
self.layoutIfNeeded()
self.textStorage.enumerateAttribute(.attachment, in: .init(location: 0, length: self.textStorage.length)) { [weak self] value, range, _ in
guard let self = self, let value = value as? NSTextAttachment, let info = value.rzattachmentInfo else { return }
let frame = self.qfistRect(for: range)
var frame = self.qfistRect(for: range)
/// 某些机型可能会出现无法获取位置,导致计算错误,这个地方使用这个兜底
if frame.origin.x.isInfinite || frame.origin.x.isNaN {
frame = .init(x: 5, y: 0, width: 0, height: 0)
}
var size: CGSize?
let lineWidth = self.frame.size.width - frame.minX - 5 // 当前行附件可显示的最大宽度
let edgeinsets = (info.infoLayer.subviews.first { v -> Bool in
Expand Down
2 changes: 2 additions & 0 deletions RZRichTextView/Classes/RZRichTextViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ open class RZRichTextViewModel: NSObject {
open weak var textView: RZRichTextView?
/// 默认字体样式, 如果需要修改,直接设置
open lazy var defaultTypingAttributes: [NSAttributedString.Key : Any] = [:]
/// 默认链接字体样式, 如果需要修改,直接设置
open lazy var defaultLinkTypingAttributes: [NSAttributedString.Key : Any] = [.foregroundColor: UIColor.qhex(0x307bf6)]
/// 需要刷新工具栏时,调用此block
open var reloadDataWithAccessoryView: (() -> Void)?
/// 用于记录输入历史可撤回次数
Expand Down

0 comments on commit 19d2cb8

Please sign in to comment.