Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rztime committed Nov 16, 2021
1 parent 43d83a8 commit d925974
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class AddAttachmentViewController: UIViewController {
view.closeBtn.rz.tap { [weak attach, weak self] _ in
guard let attach = attach else { return }
print("删除attach:\(attach.rtInfo.range)")
self?.textView?.deleteText(for: attach.rtInfo.range)
self?.textView?.rt.deleteText(for: attach.rtInfo.range)
}

view.playBtn.rz.tap { [weak attach, weak self] _ in
Expand Down
2 changes: 1 addition & 1 deletion Example/RZRichTextView/Demo/TextViewContrller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class TextViewContrller: UIViewController {
guard let textView = textView else {
return
}
let allAttach = textView.richTextAttachments()
let allAttach = textView.rt.richTextAttachments()
// 得到所有的图片(没有视频)
let attachments = textView.attributedText.rz.images()
/// 在RZColorfulSwift 中提供几种转换成html的方式,如果有视频,可能需要参照codingToHtmlWithImagesURLSIfHad来实现视频的标签
Expand Down
1 change: 1 addition & 0 deletions RZRichTextView/Classes/Depend/RZRichTextBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ extension NSRange : RZRichTextProtocol {}
extension NSAttributedString : RZRichTextProtocol {}
extension NSParagraphStyle : RZRichTextProtocol {}
extension String : RZRichTextProtocol {}
extension UITextView : RZRichTextProtocol {}
4 changes: 2 additions & 2 deletions RZRichTextView/Classes/Depend/RZRichTextViewHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ open class RZRichTextViewHelper: NSObject, UITextViewDelegate {
// 放在主线程,保证在插入图片之后,先在textView中绘制完成,然后才能拿到正确的图片位置
self.textView?.setNeedsLayout()
guard let textView = self.textView else { return }
let attachments: [NSTextAttachment]? = textView.richTextAttachments()
let attachments: [NSTextAttachment]? = textView.rt.richTextAttachments()
attachments?.forEach { [weak self] attach in
guard let self = self, let textView = self.textView else { return }
let view = attach.rtInfo.maskView
let rect = textView.rectFor(range: attach.rtInfo.range)
let rect = textView.rt.rectFor(range: attach.rtInfo.range)
view.frame = rect
if view.superview != textView {
textView.addSubview(view)
Expand Down
30 changes: 16 additions & 14 deletions RZRichTextView/Classes/RZRichTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ open class RZRichTextView: UITextView, UITextViewDelegate {
super.typingAttributes = newValue
}
get {
var type = super.typingAttributes
var type: [NSAttributedString.Key : Any] = super.typingAttributes
type.removeValue(forKey: .rt.originfontName) // 需要修正移除NSOriginalFont
let range = self.selectedRange
let tab = self.attributedText.rt.tabStyleFor(range)
Expand Down Expand Up @@ -73,37 +73,39 @@ open class RZRichTextView: UITextView, UITextViewDelegate {
}
}

public extension UITextView {
public extension RZRichTextBase where T : UITextView {
/// 获取range对应的frame
func rectFor(range: NSRange?) -> CGRect {
guard let range = range else {
return .zero
}
let beginning = self.beginningOfDocument
guard let star = self.position(from: beginning, offset: range.location) else { return .zero }
guard let end = self.position(from: star, offset: range.length) else { return .zero}
guard let textRange = self.textRange(from: star, to: end) else { return .zero}
return self.firstRect(for: textRange)
let textView = self.rt
let beginning = textView.beginningOfDocument
guard let star = textView.position(from: beginning, offset: range.location) else { return .zero }
guard let end = textView.position(from: star, offset: range.length) else { return .zero}
guard let textRange = textView.textRange(from: star, to: end) else { return .zero}
return textView.firstRect(for: textRange)
}
func deleteText(for range: NSRange?) {
if let textrange = self.textRange(for: range) {
self.replace(textrange, withText: "")
self.rt.replace(textrange, withText: "")
}
}
func textRange(for range: NSRange?) -> UITextRange?{
guard let range = range else {
return nil
}
let beginning = self.beginningOfDocument
guard let star = self.position(from: beginning, offset: range.location) else { return nil }
guard let end = self.position(from: star, offset: range.length) else { return nil }
guard let textRange = self.textRange(from: star, to: end) else { return nil }
let textView = self.rt
let beginning = textView.beginningOfDocument
guard let star = textView.position(from: beginning, offset: range.location) else { return nil }
guard let end = textView.position(from: star, offset: range.length) else { return nil }
guard let textRange = textView.textRange(from: star, to: end) else { return nil }
return textRange
}
/// 获取附件文本
@objc func richTextAttachments() -> [NSTextAttachment] {
func richTextAttachments() -> [NSTextAttachment] {
var attachments: [NSTextAttachment] = []
self.attributedText.enumerateAttribute(.attachment, in: .init(location: 0, length: attributedText.length), options: .longestEffectiveRangeNotRequired) { value, range, _ in
self.rt.attributedText.enumerateAttribute(.attachment, in: .init(location: 0, length: self.rt.attributedText.length), options: .longestEffectiveRangeNotRequired) { value, range, _ in
if let value = value as? NSTextAttachment {
value.rtInfo.range = range
attachments.append(value)
Expand Down

0 comments on commit d925974

Please sign in to comment.