LRRichEditorView 5.0.9

LRRichEditorView 5.0.9

1989925kdash 维护。



RichEditorView

License: BSD 3 Cocoapods Carthage compatible

RichEditorView 是一个简单的、模块化的、直接使用的 UIView 子类,用于富文本编辑。

使用 Swift 4 编写

通过 Cocoapods 或 Carthage 支持 iOS 8+

已见成效

Demo

只需克隆项目,然后在 Xcode 中打开 RichEditorViewSample/RichEditorViewSample.xcworkspace

功能

Toolbar Demo

  • 粗体
  • 斜体
  • 下标
  • 上标
  • 删除线
  • 下划线
  • 左对齐
  • 居中对齐
  • 右对齐
  • 标题 1
  • 标题 2
  • 标题 3
  • 标题 4
  • 标题 5
  • 标题 6
  • 撤销
  • 重做
  • 有序列表
  • 无序列表
  • 缩进
  • 缩出
  • 插入图片
  • 插入链接
  • 文字颜色
  • 文字背景色

安装

Cocoapods

如果已安装 Cocoapods,您可以使用 Cocoapods 将 RichEditorView 包含到项目中。在您的 Podfile 中添加以下内容:

pod "RichEditorView"
use_frameworks!

注意:use_frameworks! 对于 Swift 语言的库是必需的。

Carthage

在您的 Cartfile 中添加以下内容:

github 'cjwirth/RichEditorView'

使用 RichEditorView

RichEditorView 对您在应用程序中使用它的方式没有任何假设。它是一个普通的 UIView 子类,因此您可以在任何地方以任何方式使用它。

最基本的使用

editor = RichEditorView(frame: self.view.bounds)
editor.html = "<h1>My Awesome Editor</h1>Now I am editing in <em>style.</em>"
self.view.addSubview(editor)

编辑文本

要更改当前选中文本的样式,只需直接在 RichEditorView 上调用方法即可。

editor.bold()
editor.italic()
editor.setTextColor(.red)

如果您想显示编辑工具栏 RichEditorToolbar,您需要处理其显示(示例项目中的 KeyboardManager.swift 是一个不错的起点)。但是配置它就像告诉它要启用哪些选项,以及要处理哪个 RichEditorView 一样简单。

let toolbar = RichEditorToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 44))
toolbar.options = RichEditorDefaultOption.all
toolbar.editor = editor // Previously instantiated RichEditorView

某些操作需要用户提供反馈(例如选择图片、选择颜色等)。在这种情况下,您可以遵守 RichEditorToolbarDelegate 协议并响应对这些操作,并可能显示一些自定义用户界面。例如,从示例项目中,我们只需选择一个随机颜色。

private func randomColor() -> UIColor {
    let colors: [UIColor] = [
        .red, .orange, .yellow,
        .green, .blue, .purple
    ]

    let color = colors[Int(arc4random_uniform(UInt32(colors.count)))]
    return color
}

func richEditorToolbarChangeTextColor(toolbar: RichEditorToolbar) {
    let color = randomColor()
    toolbar.editor?.setTextColor(color)
}

高级编辑

如果您需要更灵活的选项,您可以通过创建符合《RichEditorOption 协议的对象,或配置一个 RichEditorOptionItem 对象,并将其添加到工具栏的选项中,来添加完全自定义的操作。

let clearAllItem = RichEditorOptionItem(image: UIImage(named: "clear"), title: "Clear") { toolbar in
    toolbar?.editor?.html = ""
    return
}
toolbar.options = [clearAllItem]

作者

凯撒·韦瑟 - [email protected]

@cjwirth on Twitter @cjwirth

致谢

许可证

RichEditorView 是在 BSD 3-Clause 许可证下发布的。有关详细信息,请参阅 LICENSE.md