Skip to content

Commit

Permalink
优化:附件在可显示区域发生变化时,按比例缩放
Browse files Browse the repository at this point in the history
  • Loading branch information
rztime committed Oct 23, 2023
1 parent fd64654 commit 403f9d8
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Example/RZRichTextView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDC1AFB9204008FA782 /* Images.xcassets */; };
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; };
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACEB1AFB9204008FA782 /* Tests.swift */; };
B22BB38F2AE236C40059A609 /* NormalTestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B22BB38E2AE236C40059A609 /* NormalTestViewController.swift */; };
B289C2182A78B0FB00D6CEC5 /* HowToUseDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B289C2172A78B0FB00D6CEC5 /* HowToUseDemo.swift */; };
B289C21A2A78B48D00D6CEC5 /* CustomRichTextViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B289C2192A78B48D00D6CEC5 /* CustomRichTextViewController.swift */; };
B289C21E2A7B7D2600D6CEC5 /* LabelLoadHtmlViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B289C21D2A7B7D2600D6CEC5 /* LabelLoadHtmlViewController.swift */; };
Expand Down Expand Up @@ -52,6 +53,7 @@
66120FD71FB9FAF42C570902 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
7C0FE6C166E24BCECD07B8C6 /* Pods_RZRichTextView_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RZRichTextView_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8D54757EF00239AD9298D3AD /* Pods-RZRichTextView_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RZRichTextView_Example.debug.xcconfig"; path = "Target Support Files/Pods-RZRichTextView_Example/Pods-RZRichTextView_Example.debug.xcconfig"; sourceTree = "<group>"; };
B22BB38E2AE236C40059A609 /* NormalTestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NormalTestViewController.swift; sourceTree = "<group>"; };
B26BEF132A837A4800C3F34B /* UpdataLog.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = UpdataLog.md; path = ../UpdataLog.md; sourceTree = "<group>"; };
B289C2172A78B0FB00D6CEC5 /* HowToUseDemo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HowToUseDemo.swift; sourceTree = "<group>"; };
B289C2192A78B48D00D6CEC5 /* CustomRichTextViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomRichTextViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -176,6 +178,7 @@
B289C2192A78B48D00D6CEC5 /* CustomRichTextViewController.swift */,
B289C21D2A7B7D2600D6CEC5 /* LabelLoadHtmlViewController.swift */,
B289C2252A81CB7D00D6CEC5 /* TestViewController.swift */,
B22BB38E2AE236C40059A609 /* NormalTestViewController.swift */,
B2DBFBC02AD4ED38001AA5C9 /* TableViewViewController.swift */,
);
name = Demo;
Expand Down Expand Up @@ -375,6 +378,7 @@
buildActionMask = 2147483647;
files = (
B289C21E2A7B7D2600D6CEC5 /* LabelLoadHtmlViewController.swift in Sources */,
B22BB38F2AE236C40059A609 /* NormalTestViewController.swift in Sources */,
B2DBFBC12AD4ED38001AA5C9 /* TableViewViewController.swift in Sources */,
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */,
B289C2262A81CB7D00D6CEC5 /* TestViewController.swift in Sources */,
Expand Down
35 changes: 35 additions & 0 deletions Example/RZRichTextView/NormalTestViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// NormalTestViewController.swift
// RZRichTextView_Example
//
// Created by rztime on 2023/10/20.
// Copyright © 2023 CocoaPods. All rights reserved.
//

import UIKit
import QuicklySwift
import RZColorfulSwift

class NormalTestViewController: UIViewController {
let textView = UITextView().qbackgroundColor(.lightGray)
override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = .white
self.view.qbody([
textView.qmakeConstraints({ make in
make.left.right.equalToSuperview().inset(10)
make.top.equalToSuperview().inset(100)
make.height.equalTo(400)
})
])

textView.rz.colorfulConfer { confer in
confer.text("!1111111111")?.font(.systemFont(ofSize: 16))
confer.image(.qimageBy(color: .red, size: .init(width: 1000, height: 100)))
}
textView.layoutIfNeeded()
}


}
3 changes: 2 additions & 1 deletion Example/RZRichTextView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class ViewController: UIViewController {
(CustomRichTextViewController.self, "自定义编辑器"),
(LabelLoadHtmlViewController.self, "UILabel加载html"),
(TestViewController.self, "测试"),
(TableViewViewController.self, "列表")
(TableViewViewController.self, "列表"),
(NormalTestViewController.self, "正常TextView")
]


Expand Down
6 changes: 3 additions & 3 deletions RZRichTextView/Classes/LabelRZRich.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public extension UILabel {
self.attributedText = attr
}
}
private var htmlkey: UInt8 = 2
public extension UILabel {
private static var htmlkey = "rzrichhtmlkey"
/// 将html转换为富文本时,设置的内容
var html: String? {
set {
objc_setAssociatedObject(self, &UILabel.htmlkey, newValue, .OBJC_ASSOCIATION_COPY)
objc_setAssociatedObject(self, &htmlkey, newValue, .OBJC_ASSOCIATION_COPY)
}
get {
if let res = objc_getAssociatedObject(self, &UILabel.htmlkey) as? String {
if let res = objc_getAssociatedObject(self, &htmlkey) as? String {
return res
}
return nil
Expand Down
10 changes: 7 additions & 3 deletions RZRichTextView/Classes/RZAttachmentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,20 @@ public class RZAttachmentInfo: NSObject {
}
}

private var attachmentinfokey: UInt8 = 1

/// 给附件添加信息
public extension NSTextAttachment {
static var attachmentinfokey = "attachmentinfokey"
fileprivate struct RZAttachmentKey {
static var attachmentinfokey = "attachmentinfokey"
}
/// 用于编辑时,附件相关信息存取
var rzattachmentInfo: RZAttachmentInfo? {
set {
objc_setAssociatedObject(self, &NSTextAttachment.attachmentinfokey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
objc_setAssociatedObject(self, &attachmentinfokey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
if let obj = objc_getAssociatedObject(self, &NSTextAttachment.attachmentinfokey) as? RZAttachmentInfo {
if let obj = objc_getAssociatedObject(self, &attachmentinfokey) as? RZAttachmentInfo {
return obj
}
return nil
Expand Down
14 changes: 11 additions & 3 deletions RZRichTextView/Classes/RZAttachmentInfoLayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public protocol RZAttachmentInfoLayerProtocol: NSObjectProtocol {
var dispose: NSObject {get set}
/// 显示音频文件名 默认true
var showAudioName: Bool {get set}
/// 图片或者音频view上下左右边距
var imageViewEdgeInsets: UIEdgeInsets { get }
}
open class RZAttachmentInfoLayerView: UIView, RZAttachmentInfoLayerProtocol {
public var operation: QuicklySwift.QPublish<RZAttachmentOperation> = .init(value: .none)
Expand Down Expand Up @@ -141,7 +143,10 @@ open class RZAttachmentInfoLayerView: UIView, RZAttachmentInfoLayerProtocol {
let contentView: UIView = .init()

public var dispose: NSObject = .init()

/// 图片或者音频view上下左右边距
public var imageViewEdgeInsets: UIEdgeInsets {
return .init(top: 15, left: 3, bottom: 0, right: 15)
}
/// 0.0-1.0
public func updateProgress(_ progress: CGFloat) {
var bounds = self.progressView.bounds
Expand All @@ -155,7 +160,9 @@ open class RZAttachmentInfoLayerView: UIView, RZAttachmentInfoLayerProtocol {
let stackView = [imageContent, audioContent].qjoined(aixs: .vertical, spacing: 0, align: .fill, distribution: .equalSpacing)
self.qbody([
stackView.qmakeConstraints({ make in
make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 10, left: 5, bottom: 5, right: 10))
make.left.equalToSuperview().inset(3)
make.top.right.equalToSuperview().inset(15)
make.bottom.lessThanOrEqualToSuperview()
}),
contentView.qmakeConstraints({ make in
make.edges.equalToSuperview()
Expand All @@ -177,7 +184,8 @@ open class RZAttachmentInfoLayerView: UIView, RZAttachmentInfoLayerProtocol {
])
imageContent.qbody([
imageView.qmakeConstraints({ make in
make.edges.equalToSuperview()
make.top.left.right.equalToSuperview()
make.bottom.lessThanOrEqualToSuperview()
}),
playBtn.qmakeConstraints({ make in
make.center.equalToSuperview()
Expand Down
8 changes: 3 additions & 5 deletions RZRichTextView/Classes/RZCss.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import UIKit
import RZColorfulSwift

private var paragraphTextListname: UInt8 = 3
public extension NSParagraphStyle {
fileprivate struct RZTextListTypeName {
static var name = "RZTextListTypeName"
}
enum RZTextListType: Int {
case none //
case ol // 有序
Expand All @@ -20,10 +18,10 @@ public extension NSParagraphStyle {
/// 有序无序状态
var rzTextListType: RZTextListType {
set {
objc_setAssociatedObject(self, &RZTextListTypeName.name, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
objc_setAssociatedObject(self, &paragraphTextListname, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
if let value = objc_getAssociatedObject(self, &RZTextListTypeName.name) as? RZTextListType {
if let value = objc_getAssociatedObject(self, &paragraphTextListname) as? RZTextListType {
return value
}
return .none
Expand Down
51 changes: 47 additions & 4 deletions RZRichTextView/Classes/RZRichTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ open class RZRichTextView: UITextView {
switch self.viewModel.showcountType {
case .hidden:
inputCountLabel.isHidden = true
self.contentInset = .init(top: 5, left: 5, bottom: 5, right: 5)
self.contentInset = .init(top: 5, left: 3, bottom: 5, right: 0)
case .showcount, .showcountandall:
inputCountLabel.isHidden = false
self.contentInset = .init(top: 5, left: 5, bottom: 20, right: 5)
self.contentInset = .init(top: 5, left: 3, bottom: 20, right: 0)
}
if #available(iOS 11.0, *) {
self.contentInsetAdjustmentBehavior = .never
Expand Down Expand Up @@ -378,8 +378,50 @@ public extension RZRichTextView {
}
/// 插入了附件之后,需要fix附件相关的界面
func fixAttachmentInfo() {
/// 修正一下附件在textView中的位置,当设置了列表的时候,附件显示宽度被压缩,此时重新设置一下附件宽高
func fixAttachmentBounds() {
self.textStorage.ensureAttributesAreFixed(in: .init(location: 0, length: self.textStorage.length))
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 size: CGSize?
let lineWidth = self.frame.size.width - frame.minX - 5 // 当前行附件可显示的最大宽度
let edgeinsets = (info.infoLayer.subviews.first { v -> Bool in
if let _ = v as? RZAttachmentInfoLayerProtocol {
return true
}
return false
} as? RZAttachmentInfoLayerProtocol)?.imageViewEdgeInsets ?? .init(top: 15, left: 3, bottom: 0, right: 15)
switch info.type {
case .image, .video:
if let image = info.image {
let imageMaxWidth = lineWidth - (edgeinsets.left + edgeinsets.right) // 图片可显示的最大宽度
let imageSize = image.size.qscaleto(maxWidth: imageMaxWidth) // 图片真实size
let realSize = CGSize.init(width: imageSize.width + (edgeinsets.left + edgeinsets.right), height: imageSize.height + (edgeinsets.top + edgeinsets.bottom)) // 附件的真实size(是由图片+边距计算得到的)
if abs(frame.size.width - realSize.width) > 5 || abs(frame.size.height - realSize.height) > 10 {
size = realSize
}
}
case .audio:
let h = self.viewModel.audioAttachmentHeight + (edgeinsets.top + edgeinsets.bottom)
if abs(lineWidth - frame.size.width) > 5 || abs(frame.size.height - h) > 10 {
size = .init(width: lineWidth, height: h)
}
}
if let size = size, size.width > 0 {
value.bounds = .init(origin: .zero, size: size)
let attr = NSMutableAttributedString.init(attributedString: .init(attachment: value))
attr.addAttributes(self.textStorage.attributes(at: range.location, effectiveRange: nil), range: .init(location: 0, length: attr.length))
let selctedRange = self.selectedRange
self.textStorage.replaceCharacters(in: range, with: attr)
self.layoutIfNeeded()
self.selectedRange = selctedRange
}
}
}
/// 将相关方法延迟加载,是为了在附件绘制完成之后,在获取位置
func fixAttachment() {
func fixAttachmentViewFrame() {
self.textStorage.ensureAttributesAreFixed(in: .init(location: 0, length: self.textStorage.length))
self.layoutIfNeeded()
var changed = false
Expand Down Expand Up @@ -436,8 +478,9 @@ public extension RZRichTextView {
self.viewModel.attachmentInfoChanged?(newattachments)
}
}
fixAttachmentBounds()
DispatchQueue.main.async {
fixAttachment()
fixAttachmentViewFrame()
self.fixTextlistNum()
}
}
Expand Down

0 comments on commit 403f9d8

Please sign in to comment.