Skip to content

Commit

Permalink
修正textList有序无序富文本导致的附件、光标位置错乱的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rztime committed Oct 8, 2023
1 parent bd84f1d commit 19566e3
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 34 deletions.
10 changes: 8 additions & 2 deletions Example/RZRichTextView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,12 @@
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = LBSWTUGGNB;
LastSwiftMigration = 0900;
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = LBSWTUGGNB;
LastSwiftMigration = 0900;
TestTargetID = 607FACCF1AFB9204008FA782;
};
Expand Down Expand Up @@ -522,11 +524,12 @@
baseConfigurationReference = 8D54757EF00239AD9298D3AD /* Pods-RZRichTextView_Example.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = LBSWTUGGNB;
INFOPLIST_FILE = RZRichTextView/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_BUNDLE_IDENTIFIER = com.rztime.RZRichTextViewExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
Expand All @@ -538,11 +541,12 @@
baseConfigurationReference = 41BF27BAD5B83D7B0517DA18 /* Pods-RZRichTextView_Example.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = LBSWTUGGNB;
INFOPLIST_FILE = RZRichTextView/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_BUNDLE_IDENTIFIER = com.rztime.RZRichTextViewExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
Expand All @@ -553,6 +557,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 3D630A2A4352A67F32D7C55C /* Pods-RZRichTextView_Tests.debug.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = LBSWTUGGNB;
FRAMEWORK_SEARCH_PATHS = (
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -576,6 +581,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = FF008A67C39722CA3E80D13D /* Pods-RZRichTextView_Tests.release.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = LBSWTUGGNB;
FRAMEWORK_SEARCH_PATHS = (
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
"$(inherited)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,4 @@
uuid = "E8427B4B-66FF-4232-9998-B98861F0525A"
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "AEFF947E-77CA-461A-B134-C405AB30EE4E"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "RZRichTextView/HTML2AttrViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "52"
endingLineNumber = "52"
landmarkName = "viewDidLoad()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
2 changes: 1 addition & 1 deletion Example/RZRichTextView/HowToUseDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public extension RZRichTextViewModel {
viewModel?.textView?.removeAttachment(info)
case .preview(let info):// 预览
// FIXME: 此处自行实现预览音视频图片的功能, 重新编辑时,取src等数据
if let allattachments = viewModel?.textView?.attachments {
if let allattachments = viewModel?.textView?.attachments.filter({$0.asset != nil}) {
if let index = allattachments.firstIndex(where: {$0 == info}) {
// 预览播放
let vc = TZPhotoPreviewController.init()
Expand Down
57 changes: 56 additions & 1 deletion RZRichTextView/Classes/RZCss.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,66 @@ import UIKit
import RZColorfulSwift

public extension NSParagraphStyle {
fileprivate struct RZTextListTypeName {
static var name = "RZTextListTypeName"
}
enum RZTextListType: Int {
case none //
case ol // 有序
case ul // 无序
}
/// 有序无序状态
var rzTextListType: RZTextListType {
set {
objc_setAssociatedObject(self, &RZTextListTypeName.name, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
if let value = objc_getAssociatedObject(self, &RZTextListTypeName.name) as? RZTextListType {
return value
}
return .none
}
}
/// 是否是有序段落
var isol: Bool {
if self.rzTextListType == .ol {
return true
}
var temp = false
self.textLists.forEach { item in
if item.markerFormat.rawValue.contains(NSTextList.MarkerFormat.decimal.rawValue) {
temp = true
}
}
if self.firstLineHeadIndent > 30.08 && self.firstLineHeadIndent < 30.12 &&
self.headIndent > 30.08 && self.headIndent < 30.12{
temp = true
}
return temp
}
/// 是否是无序
var isul: Bool {
if self.rzTextListType == .ul {
return true
}
var temp = false
self.textLists.forEach { item in
if item.markerFormat.rawValue.contains(NSTextList.MarkerFormat.disc.rawValue) {
temp = true
}
}
if self.firstLineHeadIndent > 30.28 && self.firstLineHeadIndent < 30.32 &&
self.headIndent > 30.28 && self.headIndent < 30.32{
temp = true
}
return temp
}
/// 将paragraph转换为css对应的样式
func rz2cssStyle(font: UIFont?) -> String {
var styles: [String] = []
styles.append("margin:\(self.paragraphSpacingBefore)px 0.0px \(self.paragraphSpacing)px \(self.headIndent)px;")
if !self.isol && !self.isul {
styles.append("margin:\(self.paragraphSpacingBefore)px 0.0px \(self.paragraphSpacing)px \(self.headIndent)px;")
}
switch self.alignment {
case .left, .justified, .natural: break
case .center: styles.append("text-align:center;")
Expand All @@ -47,6 +83,25 @@ public extension NSParagraphStyle {
return styles.joined()
}
}
public extension NSMutableParagraphStyle {
/// 设置列表样式
func setTextListType(_ type: NSParagraphStyle.RZTextListType) {
self.rzTextListType = type
self.textLists = []
// 30.1 30.3浮点需要,主要用于区分
switch type {
case .none:
self.firstLineHeadIndent = 0
self.headIndent = 0
case .ol:
self.firstLineHeadIndent = 30.1
self.headIndent = 30.1
case .ul:
self.firstLineHeadIndent = 30.3
self.headIndent = 30.3
}
}
}

public extension [NSAttributedString.Key: Any] {
var rz2cssStyle: String {
Expand Down
24 changes: 16 additions & 8 deletions RZRichTextView/Classes/RZHtml.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ public struct RZRichTempAttributedString {
let isul: Bool // 无序
let isol: Bool // 有序
}
/// 有序 .decimal
public let rz_ol: [NSTextList] = [.init(markerFormat: .decimal, options: 0)]
/// 无序 .disc
public let rz_ul: [NSTextList] = [.init(markerFormat: .disc, options: 0)]

public extension RZRichTextView {
/// 转换时,需要确认附件是否真的上传完成,否则src、poster可能为空
func code2html() -> String {
if self.textStorage.length == 0 {
return ""
}
return self.textStorage.code2Html(self.viewModel.spaceRule)
}
/// 重新编辑时,将html转换为NSAttributedString
Expand Down Expand Up @@ -127,10 +126,16 @@ public extension NSAttributedString {
let lastpol = idx > 0 ? (tempAllparagraphContents[qsafe: idx - 1]?.isol ?? false) : false
let currentpol = p.isol
let nextpol = tempAllparagraphContents[qsafe: idx + 1]?.isol ?? false

let para = p.content.rt.paragraphstyle
var style = "<li>"
if para?.alignment == .center {
style = #"<li style="text-align:center;">"#
} else if para?.alignment == .right {
style = #"<li style="text-align:right;">"#
}
if currentpul && !lastpul { realParagraphs.append("<ul>") }
if currentpol && !lastpol { realParagraphs.append("<ol>") }
if currentpul || currentpol { realParagraphs.append("<li>") }
if currentpul || currentpol { realParagraphs.append(style) }
realParagraphs.append(p.content)
if currentpul || currentpol { realParagraphs.append("</li>") }
if currentpol && !nextpol { realParagraphs.append("</ol>") }
Expand Down Expand Up @@ -313,8 +318,11 @@ public extension String {
mp.paragraphSpacing = p.paragraphSpacing
mp.headIndent = p.headIndent
mp.firstLineHeadIndent = p.firstLineHeadIndent
if p.isol { mp.textLists = rz_ol }
else if p.isul { mp.textLists = rz_ul }
if p.isol {
mp.setTextListType(.ol)
} else if p.isul {
mp.setTextListType(.ul)
}
switch p.alignment {
case .center, .right: mp.alignment = p.alignment
case .left, .natural, .justified: mp.alignment = .left
Expand Down
4 changes: 2 additions & 2 deletions RZRichTextView/Classes/RZRichTableStyleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ open class RZRichTableStyleView: UIView {
var img: UIImage?
switch type {
case .t_ol:
mutablePara.textLists = btn.isSelected ? rz_ol : []
mutablePara.setTextListType(btn.isSelected ? .ol : .none)
img = toolImage(type: .t_ol)
case .t_ul:
mutablePara.textLists = btn.isSelected ? rz_ul : []
mutablePara.setTextListType(btn.isSelected ? .ul : .none)
img = toolImage(type: .t_ul)
default: break
}
Expand Down
Loading

0 comments on commit 19566e3

Please sign in to comment.