Skip to content

Commit

Permalink
1.0.0 优化url相关,命名规范
Browse files Browse the repository at this point in the history
  • Loading branch information
rztime committed Nov 16, 2021
1 parent 54287a6 commit a067825
Show file tree
Hide file tree
Showing 53 changed files with 1,105 additions and 150 deletions.
8 changes: 8 additions & 0 deletions Example/RZRichTextView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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 */; };
B20439572741FFB5001D8614 /* CustomerTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20439562741FFB5001D8614 /* CustomerTextView.swift */; };
B204395927420779001D8614 /* CRichTextFontStyleOptionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B204395827420779001D8614 /* CRichTextFontStyleOptionsView.swift */; };
B21BC2692730D5ED0051ADB1 /* CustomCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B21BC2682730D5ED0051ADB1 /* CustomCell.swift */; };
B21BC26B273112800051ADB1 /* OrderdTextViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B21BC26A273112800051ADB1 /* OrderdTextViewController.swift */; };
B22B618B272821EB00E3F68A /* TextViewContrller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B22B618A272821EB00E3F68A /* TextViewContrller.swift */; };
Expand Down Expand Up @@ -50,6 +52,8 @@
607FACEB1AFB9204008FA782 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
67BA4762C3656B6325489C4D /* RZRichTextView.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = RZRichTextView.podspec; path = ../RZRichTextView.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
9402DB31A4D8FF59AB92B081 /* Pods-RZRichTextView_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RZRichTextView_Tests.debug.xcconfig"; path = "Target Support Files/Pods-RZRichTextView_Tests/Pods-RZRichTextView_Tests.debug.xcconfig"; sourceTree = "<group>"; };
B20439562741FFB5001D8614 /* CustomerTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomerTextView.swift; sourceTree = "<group>"; };
B204395827420779001D8614 /* CRichTextFontStyleOptionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CRichTextFontStyleOptionsView.swift; sourceTree = "<group>"; };
B21BC2682730D5ED0051ADB1 /* CustomCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomCell.swift; sourceTree = "<group>"; };
B21BC26A273112800051ADB1 /* OrderdTextViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderdTextViewController.swift; sourceTree = "<group>"; };
B22B618A272821EB00E3F68A /* TextViewContrller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextViewContrller.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -176,6 +180,7 @@
isa = PBXGroup;
children = (
B21BC2682730D5ED0051ADB1 /* CustomCell.swift */,
B204395827420779001D8614 /* CRichTextFontStyleOptionsView.swift */,
);
path = View;
sourceTree = "<group>";
Expand All @@ -188,6 +193,7 @@
B25DD05E2728E7AF007AE34A /* CustomToolBarViewController.swift */,
B21BC26A273112800051ADB1 /* OrderdTextViewController.swift */,
B2EB98C3273B742E008B4C88 /* AddAttachmentViewController.swift */,
B20439562741FFB5001D8614 /* CustomerTextView.swift */,
);
path = Demo;
sourceTree = "<group>";
Expand Down Expand Up @@ -383,13 +389,15 @@
buildActionMask = 2147483647;
files = (
B22B618B272821EB00E3F68A /* TextViewContrller.swift in Sources */,
B20439572741FFB5001D8614 /* CustomerTextView.swift in Sources */,
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */,
B21BC2692730D5ED0051ADB1 /* CustomCell.swift in Sources */,
B21BC26B273112800051ADB1 /* OrderdTextViewController.swift in Sources */,
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */,
B25DD05F2728E7AF007AE34A /* CustomToolBarViewController.swift in Sources */,
B2EB98C8273BBBA9008B4C88 /* TestViewController.m in Sources */,
B2EB98C4273B742E008B4C88 /* AddAttachmentViewController.swift in Sources */,
B204395927420779001D8614 /* CRichTextFontStyleOptionsView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
16 changes: 8 additions & 8 deletions Example/RZRichTextView/Demo/AddAttachmentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,29 @@ class AddAttachmentViewController: UIViewController {
// 插入了附件,这里需要对遮罩上绑定的自定义view,进行防止重复加载处理,因为在实际编写过程中,可能会出现删除,撤销操作,导致此方法重新调用
option.didInsetAttachment = { [weak self] attach in
// 存在重复添加的情况,比如通过撤销操作,还原了已输入的图片,这时候就需要重新布局,重新绑定上传状态等等
let view = attach.rzrt.maskView.viewWithTag(100) as? RZUploadStatusView ?? .init()
attach.rzrt.maskView.addSubview(view)
let view = attach.rtInfo.maskView.viewWithTag(100) as? RZUploadStatusView ?? .init()
attach.rtInfo.maskView.addSubview(view)
view.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
view.playBtn.isHidden = attach.rzrt.type != .video
view.playBtn.isHidden = attach.rtInfo.type != .video
view.closeBtn.rz.tap { [weak attach, weak self] _ in
guard let attach = attach else { return }
print("删除attach:\(attach.rzrt.range)")
self?.textView?.deleteText(for: attach.rzrt.range)
print("删除attach:\(attach.rtInfo.range)")
self?.textView?.deleteText(for: attach.rtInfo.range)
}

view.playBtn.rz.tap { [weak attach, weak self] _ in
self?.preVideo(phasset: attach?.rzrt.asset)
self?.preVideo(phasset: attach?.rtInfo.asset)
}
/// 这里禁掉,主要是方便直接预览图片和视频,实际看个人需要
view.playBtn.isUserInteractionEnabled = false
// 预览
view.rz.tap { [weak self, weak attach] _ in
self?.preVideo(phasset: attach?.rzrt.asset)
self?.preVideo(phasset: attach?.rtInfo.asset)
}

var item: RZUploadStatusItem? = self?.viewModel.items.first(where: { $0.attach.rzrt.identifier == attach.rzrt.identifier})
var item: RZUploadStatusItem? = self?.viewModel.items.first(where: { $0.attach.rtInfo.identifier == attach.rtInfo.identifier})
if item == nil {
item = .init(attach: attach)
self?.viewModel.items.append(item!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CustomToolBarViewController: UIViewController {
// attahcment.image
// attahcment.asset
// 这是图片上的蒙层,可以添加一些进度控件、删除控件等等
attahcment.rzrt.maskView.backgroundColor = UIColor.init(red: 1, green: 0, blue: 0, alpha: 0.3)
attahcment.rtInfo.maskView.backgroundColor = UIColor.init(red: 1, green: 0, blue: 0, alpha: 0.3)
// 也可以根据需要绑定上传
}
/// 移除了附件
Expand Down
233 changes: 233 additions & 0 deletions Example/RZRichTextView/Demo/CustomerTextView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
//
// CustomerTextView.swift
// RZRichTextView_Example
//
// Created by rztime on 2021/11/15.
// Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RZRichTextView
import RxSwift
import RxCocoa

class CustomerTextView: UIViewController {

enum CToolBarItem: NSInteger {
case cimage = 100
case cfont = 101
case cvideo = 102
case clink = 103
}
var textView: RZRichTextView?

let dispose = DisposeBag()
var cheight : CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white

let options = RZRichTextViewOptions.shared

options.obliqueFont = UIFont.init(name: "XinYuGongHeXieSong-L-Light", size: 15) ?? .italicSystemFont(ofSize: 15) // 斜体
options.boldObliqueFont = .init(name: "XinYuGongHeXieSong-B-Bold", size: 15) ?? .boldSystemFont(ofSize: 15) // 粗斜体

options.icon_revoke = CRichTextConfigure.revokeIcon
options.icon_restore = CRichTextConfigure.reStoreIcon
options.icon_revokeEnable = nil
options.icon_restoreEnable = nil
// 不需要移除link属性
options.shouldRemoveLinkWhenEditing = false

options.colors = [
.init(161, 161, 161, 1),
.init(117, 117, 117, 1),
.init(25, 25, 25, 1),
.init(255, 246, 0, 1),
.init(255, 0, 0, 1),
.init(3, 132, 225, 1),
.init(0, 236, 28, 1),
.init(120, 0, 255, 1),
]

let tooleBars: [RZToolBarItem] = [
.init(type: CToolBarItem.cimage.rawValue, image: CRichTextConfigure.imageIcon, selectedImage: nil, selected: false, exparams: nil, items: nil),
.init(type: CToolBarItem.cfont.rawValue, image: CRichTextConfigure.fontIcon, selectedImage: CRichTextConfigure.fontHightIcon, selected: false, exparams: nil, items: nil),
.init(type: CToolBarItem.cvideo.rawValue, image: CRichTextConfigure.videoIcon, selectedImage: nil, selected: false, exparams: nil, items: nil),
.init(type: CToolBarItem.clink.rawValue, image: CRichTextConfigure.linkIcon, selectedImage: nil, selected: false, exparams: nil, items: nil),
]
options.toolbarItems = tooleBars

options.didSelectedToolbarItem = { [weak self] (item, index) in
self?.textView?.becomeFirstResponder()
switch item.type {
case CToolBarItem.cimage.rawValue:
let vc = TZImagePickerController.init(maxImagesCount: 1, delegate: nil)
vc?.allowPickingImage = true
vc?.allowPickingVideo = false
vc?.allowTakeVideo = false
vc?.allowTakePicture = false
vc?.allowCrop = false
vc?.didFinishPickingPhotosHandle = { [weak self] (photos, assets, _) in
if let image = photos?.first {
let asset = assets?.first as? PHAsset
self?.textView?.helper?.insert(text: nil, image: image, asset: asset)
}
}
RZRichTextViewUtils.rz_currentViewController()?.present(vc!, animated: true, completion: nil)
case CToolBarItem.cfont.rawValue:
if let textView = self?.textView {
item.selected = !item.selected
if item.selected {
textView.inputView = CRichTextFontStyleOptionsView.init(frame: .init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: self?.cheight ?? 0), options: textView.options, typingAttributes: textView.typingAttributes, changed: { [weak self] typingAttributes in
guard let textView = self?.textView else {
return
}
textView.typingAttributes = typingAttributes
textView.helper?.insert(text: nil, image: nil, changeParagraph:true)
})
} else {
textView.inputView?.removeFromSuperview()
textView.inputView = nil
}
textView.reloadInputViews()
}
break
case CToolBarItem.cvideo.rawValue:
let vc = TZImagePickerController.init(maxImagesCount: 1, delegate: nil)
vc?.allowPickingImage = false
vc?.allowPickingVideo = true
vc?.allowTakeVideo = false
vc?.allowTakePicture = false
vc?.allowCrop = false
vc?.didFinishPickingVideoHandle = { (image, asset) in
if let image = image {
self?.textView?.helper?.insert(text: nil, image: image, asset: asset)
}
}
RZRichTextViewUtils.rz_currentViewController()?.present(vc!, animated: true, completion: nil)
case CToolBarItem.clink.rawValue:
if let range = self?.textView?.selectedRange {
self?.changeURL(range: range, url: nil)
}
default: break
}
self?.textView?.kinputAccessoryView.reloadData()
return false
}

let textView = RZRichTextView.init(frame: .zero, options: options)

self.view.addSubview(textView)
textView.snp.makeConstraints { make in
make.left.right.equalToSuperview().inset(20)
make.top.equalToSuperview().inset(100)
make.height.equalTo(400)
}

textView.font = options.normalFont.withSize(15)
textView.textColor = options.colors[2].color

textView.backgroundColor = .lightGray
textView.kinputAccessoryView.closeBtn.isHidden = true

[textView.kinputAccessoryView.leftBtn, textView.kinputAccessoryView.rightBtn].forEach { b in
b.snp.updateConstraints { make in
make.width.equalTo(44)
}
}
self.textView = textView
textView.delegate = self

_ = NotificationCenter.default.rx.notification(UIResponder.keyboardWillChangeFrameNotification).take(until: self.rx.deallocated)
.subscribe(onNext: { [weak self] (notify) in
let keyboardFrame = (notify.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
self?.cheight = keyboardFrame.height - 44
}).disposed(by: dispose)
}
func changeURL(range: NSRange, url: URL?) {
let string = self.textView?.attributedText.attributedSubstring(from: range).string
CLinkInputViewContrller.show(text: string, link: url?.absoluteString) {[weak self] (text, url) in
guard let textView = self?.textView else {
return
}
let tempText = text ?? url
var typingAttributes = textView.typingAttributes
typingAttributes[NSAttributedString.Key.link] = url
textView.typingAttributes = typingAttributes
textView.helper?.insert(text: tempText, replaceRange: range)
}
}
}
extension CustomerTextView: UITextViewDelegate {
@available(iOS 10.0, *)
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
self.changeURL(range: characterRange, url: URL)
return false
}
}

struct CRichTextConfigure {
enum FontStyleItem: Int {
case bold = 0
case itial = 1
case underLine = 2
case deleteLine = 3
}
// 和FontStyleItem一一对应
static let fontStyleImages: [UIImage?] = [CRichTextConfigure.boldIcon, CRichTextConfigure.itialIcon, CRichTextConfigure.underLineIcon, CRichTextConfigure.deleteLineIcon]

static let imageIcon = UIImage.init(named: "rt_imageIcon")
static let fontIcon = UIImage.init(named: "rt_fontStyleIcon")
static let fontHightIcon = UIImage.init(named: "rt_fontStyleIcon_hight")
static let videoIcon = UIImage.init(named: "rt_videoIcon")
static let linkIcon = UIImage.init(named: "rt_linkIcon")
static let revokeIcon = UIImage.init(named: "rt_revokeIcon")
static let reStoreIcon = UIImage.init(named: "rt_restoreIcon")
static let boldIcon = UIImage.init(named: "rt_boldIcon")
static let itialIcon = UIImage.init(named: "rt_itailIcon")
static let underLineIcon = UIImage.init(named: "rt_underLineIcon")
static let deleteLineIcon = UIImage.init(named: "rt_deleteLineIcon")
static let aliginDefaultIcon = UIImage.init(named: "rt_aligin_default")
static let aliginLeftIcon = UIImage.init(named: "rt_aliginLeftIcon")
static let aliginCenterIcon = UIImage.init(named: "rt_aliginCenterIcon")
static let aliginrightIcon = UIImage.init(named: "rt_aliginRightIcon")

static let fontsizes : [CGFloat] = [12, 15, 17, 19]
}

class CLinkInputViewContrller: UIAlertController {
class func show(text: String?, link: String?, finish:((_ text: String?, _ link: String?) -> Void)?) {
let t = (text?.isEmpty ?? true) ? "插入超链接" : "编辑超链接"
let vc = CLinkInputViewContrller.init(title: t, message: nil, preferredStyle: .alert)
vc.addTextField { t1 in

}
vc.addTextField { t2 in

}
vc.textFields?.first?.text = link?.removingPercentEncoding
vc.textFields?.first?.placeholder = "请输入超链接地址"
vc.textFields?.last?.text = text
vc.textFields?.last?.placeholder = "请输入超链接标题"
let action0 = UIAlertAction.init(title: "放弃操作", style: .cancel) { _ in

}
let aciton1 = UIAlertAction.init(title: "确认", style: .default) { [weak vc] _ in
let link = vc?.textFields?.first?.text?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let text = vc?.textFields?.last?.text
finish?(text, (link?.isEmpty ?? true) ? nil : link)
}
let action2 = UIAlertAction.init(title: "取消超链接", style: .default) { [weak vc] _ in
let link: String? = nil
let text = vc?.textFields?.last?.text
finish?(text, link)
}
if !(text?.isEmpty ?? true) {
vc.addAction(action2)
}
vc.addAction(aciton1)
vc.addAction(action0)
RZRichTextViewUtils.rz_currentViewController()?.present(vc, animated: true, completion: nil)
}
}
6 changes: 3 additions & 3 deletions Example/RZRichTextView/Demo/OrderdTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ class OrderdTextViewController: UIViewController {
confer.text("哈哈哈")?.font(.systemFont(ofSize: 17)).textColor(.black).paragraphStyle?.addTab(.ol)
} ?? .init()
// 如果通过addTab的方式加了列表序号, 需要resetTabOrderNumber 才会排序
textView.attributedText = ori.resetTabOrderNumber()
textView.attributedText = ori.rt.resetTabOrderNumber()
}
@objc func transformTohtml() {
let html = self.textView.attributedText.rz.codingToHtmlWithImagesURLSIfHad(urls: ["http://www.baidu.com"]) ?? ""
print("\(html)")
if let attr = self.textView.attributedText {
let newattr = NSMutableAttributedString.init(attributedString: attr)
newattr.removeAttribute(.originfontName, range: .init(location: 0, length: newattr.length))
newattr.removeAttribute(.rt.originfontName, range: .init(location: 0, length: newattr.length))
self.textView.attributedText = newattr
}
}
Expand All @@ -62,7 +62,7 @@ class OrderdTextViewController: UIViewController {
extension ParagraphStyleRZ {
@discardableResult
public func addTab(_ tab: RZTextTabStyle) -> Self {
let p = paragraph.rz_transParagraphTo(tab)
let p = paragraph.rt.transParagraphTo(tab)
paragraph.setParagraphStyle(p)
return self
}
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 @@ -33,7 +33,7 @@ class TextViewContrller: UIViewController {
// attahcment.image
// attahcment.asset
// 这是图片上的蒙层,可以添加一些进度控件、删除控件等等
attahcment.rzrt.maskView.backgroundColor = UIColor.init(red: 1, green: 0, blue: 0, alpha: 0.3)
attahcment.rtInfo.maskView.backgroundColor = UIColor.init(red: 1, green: 0, blue: 0, alpha: 0.3)
// 也可以根据需要绑定上传
}
/// 移除了附件
Expand Down
Loading

0 comments on commit a067825

Please sign in to comment.