JMRichEditorView 1.0.1

JMRichEditorView 1.0.1

Jassim 维护。



  • Jassim Mukthar

RichEditorView

License: BSD 3 Cocoapods Carthage compatible

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

用 Swift 4 编写

支持 iOS 8+,通过 Cocoapods 或 Carthage。

已实现功能

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并响应这些操作,并可能显示一些自定义UI。例如,从示例项目中,我们只是选择一个随机的颜色

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]

作者

Caesar Wirth - [email protected]

@cjwirth on Twitter @cjwirth

感谢

许可

RichEditorView遵循BSD 3条款许可。请参阅LICENSE.md以获取详细说明。