Skip to content

Commit

Permalink
2.4.0 添加块引用、优化附件的加载逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
rztime committed Nov 15, 2024
1 parent b044c63 commit 57b811f
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 138 deletions.
5 changes: 3 additions & 2 deletions Example/RZRichTextView/HTML2AttrViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class HTML2AttrViewController: UIViewController {
.qtitle("转html")
.qtitleColor(.red)
.qtap { [weak self] view in
guard let self = self else { return }
self.textView.isEditable = !self.textView.isEditable
guard let self = self else { return }
let html = self.textView.code2html()
print("-----html:\n\(html)")
}

/// 上传完成时,可以点击
Expand Down
77 changes: 35 additions & 42 deletions Example/RZRichTextView/HowToUseDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,49 @@ import Kingfisher
public extension RZRichTextViewModel {
/// 如果有需要自定义实现资源下载,可以放开代码,并实现sync_imageBy、async_imageBy方法
static var configure: RZRichTextViewConfigure = {
/// 一些与textView无关的配置,不方便写在viewModel里,所以提取出来配置在configure里
let tempConfigure = RZRichTextViewConfigure.shared
/// 同步获取图片(这是一个默认实例,可以按需设置请求图片的方法,注意gif、视频首帧图)
tempConfigure.sync_imageBy = { source in
let imgView = UIImageView()
imgView.kf.setImage(with: source?.qtoURL)
return imgView.image
}
/// 异步获取图片(这是一个默认实例, 可以按需设置请求图片的方法,注意gif、视频首帧图)
tempConfigure.async_imageBy = { source, complete in
let comp = complete
guard let s = source else {
comp?(source, RZRichTextViewConfigure.shared.loadErrorImage)
return
}
var imgView : UIImageView? = .init()
imgView?.kf.setImage(with: source?.qtoURL) { result in
let image = try? result.get().image
if image == nil {
/// 图片获取失败,当做视频去请求首帧,并缓存
UIImage.qimageByVideoUrl(s) { _, image in
if let image = image {
ImageCache.default.store(image, forKey: s)
}
comp?(s, image ?? RZRichTextViewConfigure.shared.loadErrorImage)
imgView = nil
}
} else {
comp?(s, image ?? RZRichTextViewConfigure.shared.loadErrorImage)
imgView = nil
}
}
}
/// 这个颜色将生成一张图片,用于占据textView里的附件图片
tempConfigure.attachBackgroundColor = .clear
/// 支持有序无序 默认true
tempConfigure.tabEnable = true
/// 支持块 默认false
tempConfigure.quoteEnable = true
/// 块\列表只能存在一个 ,默认true,(false时可以同时存在)
tempConfigure.quoteOrTab = true
/// 无序符号配置
tempConfigure.ulSymbol = "·"
//tempConfigure.ulSymbol = "*"
tempConfigure.ulSymbolAlignment = .right
/// 如果设置,将固定无序符号的font
tempConfigure.ulSymbolFont = nil // .systemFont(ofSize: 14, weight: .medium)
/// 引用背景色
tempConfigure.quoteColor = .qhex(0xcccccc)
/// 转换为html时,blockquote的style
tempConfigure.blockquoteStyle = #"border-left: 5px solid #eeeeee;"#
// 其他配置可查看并参照RZRichTextViewConfigure
/// 同步获取图片(参照内部默认配置方法)
// tempConfigure.sync_imageBy = { source in
//
// }
/// 异步获取图片(参照内部默认配置方法)
// tempConfigure.async_imageBy = { source, complete in
//
// }
return tempConfigure
}()
class func shared(edit: Bool = true) -> RZRichTextViewModel {
/// 自定义遮罩view 默认RZAttachmentInfoLayerView
// RZAttachmentOption.register(attachmentLayer: RZAttachmentInfoLayerView.self)

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

let viewModel = RZRichTextViewModel.init()
viewModel.canEdit = edit
/// 无序符号配置
viewModel.ulSymbol = "·"
//viewModel.ulSymbol = "*"
viewModel.ulSymbolAlignment = .right
/// 如果设置,将固定无序符号的font
// viewModel.ulSymbolFont = .systemFont(ofSize: 14, weight: .medium)

/// 支持块时,插入块的入口
if configure.quoteEnable {
viewModel.inputItems.insert(.init(type: .quote, image: RZRichImage.imageWith("quote"), highlight: RZRichImage.imageWith("quote")), at: 2)
}
/// 链接颜色
viewModel.defaultLinkTypingAttributes = [.foregroundColor: UIColor.qhex(0x307bf6), .underlineColor: UIColor.qhex(0x307bf6), .underlineStyle: NSUnderlineStyle.styleSingle.rawValue]
/// 显示音频文件名字
Expand Down Expand Up @@ -103,8 +96,8 @@ public extension RZRichTextViewModel {
viewModel?.textView?.removeAttachment(info)
case .preview(let info):// 预览
// FIXME: 此处自行实现预览音视频图片的功能, 重新编辑时,取src等数据
let allattachments = viewModel?.textView?.attachments
let index = allattachments?.firstIndex(where: {$0 == info})
// let allattachments = viewModel?.textView?.attachments
// let index = allattachments?.firstIndex(where: {$0 == info})
if let asset = info.asset {
// 预览播放
let vc = TZPhotoPreviewController.init()
Expand Down
2 changes: 1 addition & 1 deletion RZRichTextView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'RZRichTextView'
s.version = '2.3.0'
s.version = '2.4.0'
s.summary = 'RZRichTextView. 原生UITextView,支持富文本输入(图片,视频,列表序号,文本颜色大小各种样式等等)'

# This description is used to generate tags and improve search results.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion RZRichTextView/Classes/RZAttachmentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public extension NSTextAttachment {
class func createWithinfo(_ info: RZAttachmentInfo) -> Self {
let attachment = Self.init()
attachment.rzattachmentInfo = info
let color = RZRichTextViewConfigure.shared.backgroundColor
let color = RZRichTextViewConfigure.shared.attachBackgroundColor
switch info.type {
case .image, .video:
let size = (info.image?.size ?? .init(width: 1, height: 1)).qscaleto(maxWidth: info.maxWidth)
Expand Down
71 changes: 58 additions & 13 deletions RZRichTextView/Classes/RZCss.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public extension NSParagraphStyle {
case ol // 有序
case ul // 无序
}
/// 块
enum RZBlockquote {
case none
case blockquote
}
/// 有序无序状态
var rzTextListType: RZTextListType {
set {
Expand Down Expand Up @@ -56,8 +61,9 @@ public extension NSParagraphStyle {
}
}
if temp { return true }
if self.firstLineHeadIndent > 30.08 && self.firstLineHeadIndent < 30.12 &&
self.headIndent > 30.08 && self.headIndent < 30.12{
let fi = Int(self.firstLineHeadIndent * 100)
let i = Int(self.headIndent * 100)
if fi == 3011 && i == 3011 {
temp = true
}
return temp
Expand All @@ -84,17 +90,24 @@ public extension NSParagraphStyle {
}
}
if temp { return true }
if self.firstLineHeadIndent > 30.28 && self.firstLineHeadIndent < 30.32 &&
self.headIndent > 30.28 && self.headIndent < 30.32{
let fi = Int(self.firstLineHeadIndent * 100)
let i = Int(self.headIndent * 100)
if fi == 3012 && i == 3012 {
temp = true
}
return temp
}
/// 块
var isblockquote: Bool {
return Int(self.tailIndent * 1000) == -11
}
/// 将paragraph转换为css对应的样式
func rz2cssStyle(font: UIFont?) -> String {
var styles: [String] = []
if !self.isol && !self.isul {
styles.append("margin:\(self.paragraphSpacingBefore)px 0.0px \(self.paragraphSpacing)px \(self.headIndent)px;")
if self.paragraphSpacingBefore != 0 || self.paragraphSpacing != 0 || self.headIndent != 0 {
styles.append("margin:\(self.paragraphSpacingBefore)px 0.0px \(self.paragraphSpacing)px \(self.headIndent)px;")
}
}
switch self.alignment {
case .left, .justified, .natural: break
Expand All @@ -103,7 +116,7 @@ public extension NSParagraphStyle {
@unknown default:
break
}
if self.tailIndent != 0 { styles.append("text-indent:\(self.tailIndent)px;") }
if !self.isblockquote, self.tailIndent != 0 { styles.append("text-indent:\(self.tailIndent)px;") }
if let font = font {
styles.append("font-size:\(font.pointSize)px;")
}
Expand All @@ -114,18 +127,50 @@ public extension NSMutableParagraphStyle {
/// 设置列表样式
func setTextListType(_ type: NSParagraphStyle.RZTextListType) {
self.rzTextListType = type
/// 块和列表是否只能二选一
let quoteOrTab = RZRichTextViewConfigure.shared.quoteOrTab
self.textLists = []
// 30.1 30.3浮点需要,主要用于区分
// 30.11 30.12浮点需要,主要用于区分
switch type {
case .none:
self.firstLineHeadIndent = 0
self.headIndent = 0
if self.isblockquote {
self.firstLineHeadIndent = 30
self.headIndent = 30
} else {
self.firstLineHeadIndent = 0
self.headIndent = 0
}
case .ol:
self.firstLineHeadIndent = 30.1
self.headIndent = 30.1
if quoteOrTab { self.tailIndent = 0 }
self.firstLineHeadIndent = 30.111
self.headIndent = 30.111
case .ul:
self.firstLineHeadIndent = 30.3
self.headIndent = 30.3
if quoteOrTab { self.tailIndent = 0 }
self.firstLineHeadIndent = 30.121
self.headIndent = 30.121
}
}
func setBlockquote(_ type: NSParagraphStyle.RZBlockquote) {
/// 块和列表是否只能二选一
let quoteOrTab = RZRichTextViewConfigure.shared.quoteOrTab
switch type {
case .none:
self.tailIndent = 0
if !(self.isol || self.isul) {
self.firstLineHeadIndent = 0
self.headIndent = 0
}
case .blockquote:
self.tailIndent = -0.0111
if quoteOrTab {
self.firstLineHeadIndent = 30
self.headIndent = 30
} else {
if !(self.isol || self.isul) {
self.firstLineHeadIndent = 30
self.headIndent = 30
}
}
}
}
}
Expand Down
Loading

0 comments on commit 57b811f

Please sign in to comment.