ZSSRichTextEditor
是一个用于 iOS
的漂亮的富文本 WYSIWYG 编辑器
。它包括您从 WYSIWYG
编辑器期望的所有标准编辑工具以及一个出色的源视图,具有语法高亮。
编辑器就像在任何其他 iOS 文本输入区域中使用一样。选择文本或内容,然后点击下面的工具栏项以应用该样式。还包括一个源视图,您可以做出更改,这些更改将在编辑器预览中反映出来。
我们希望有一个非常漂亮的颜色选取器,使更改颜色变得非常简单。因此,我们使用了开源的 HRColorPicker,这正是我们所需要的。为文本或背景选择颜色简单且无缝。
只需继承 ZSSRichTextEditor
并使用以下内容
// HTML Content to set in the editor
NSString *html = @"<!-- This is an HTML comment -->"
"<p>This is a test of the <strong>ZSSRichTextEditor</strong> by <a title=\"Zed Said\" href=\"http://www.zedsaid.com\">Zed Said Studio</a></p>";
// Set the base URL if you would like to use relative links, such as to images.
self.baseURL = [NSURL URLWithString:@"http://www.zedsaid.com"];
// If you want to pretty print HTML within the source view.
self.formatHTML = YES;
// set the initial HTML for the editor
[self setHTML:html];
如果您想要从编辑器检索 HTML
// Returns an NSString
[self getHTML];
将 HTML 插入到当前的插入点位置
NSString *html = @"<strong>I love cats!</strong>";
[self insertHTML:html];
更改工具栏按钮的色调颜色
// Set the toolbar item color
self.toolbarItemTintColor = [UIColor greenColor];
// Set the toolbar selected color
self.toolbarItemSelectedTintColor = [UIColor redColor];
只显示工具栏中的指定按钮
self.enabledToolbarItems = @[ZSSRichTextEditorToolbarBold, ZSSRichTextEditorToolbarH1, ZSSRichTextEditorToolbarParagraph];
如果您想在光标处手动插入链接或图像,可以使用以下方法
插入图像
- (void)insertImage:(NSString *)url alt:(NSString *)alt;
插入链接
- (void)insertLink:(NSString *)url title:(NSString *)title;
如果您想使用 alternat 方法定义图像和链接,您可以实现自己的选择器。例如,从您的相册上传图像后插入 URL。
当您点击备用的选择器图标(瞄准器)时,它将调用相应的方法,您需要在您的 ZSSRichTextEditor
子类中覆盖该方法(请参阅示例项目)
- (void)showInsertURLAlternatePicker {
[self dismissAlertView];
// Show your custom picker
}
- (void)showInsertImageAlternatePicker {
[self dismissAlertView];
// Show your custom picker
}
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, buttonWidth, 28.0f)];
[myButton setTitle:@"My Button" forState:UIControlStateNormal];
[myButton addTarget:self
action:@selector(didTapCustomToolbarButton:)
forControlEvents:UIControlEventTouchUpInside];
[self addCustomToolbarItemWithButton:myButton];
ZSSRichTextEditor 有以下功能
ZSSRichTextEditor
需要 iOS7 以及 CoreGraphics.framework
和 CoreText.framework
。
当在您的项目中使用 ZSSRichTextEditor
时,XCode 会自动在构建阶段将 ZSSRichTextEditor.js
添加到编译源代码中,这会导致 ZSSRichTextEditor
不能正常工作,因为javascript文件不会被包含在您的应用中。取而代之的是,将其从编译源代码中移除,并将其添加到复制资源包。
ZSSRichTextEditor
采用了以下来源的部分代码
组件 | 描述 | 许可证 |
---|---|---|
CYRTextView | CYRTextView 是一个 UITextView 子类,实现了与语法或代码文本视图相关的各种功能。 | MIT |
HRColorPicker | iPhone 简单颜色选择器 | BSD |
jQuery | jQuery 是一个快速、小巧且功能丰富的 JavaScript 库。 | MIT |
JS Beautifier | 美化丑陋的 JavaScript | MIT |
访问我们的网站 http://www.zedsaid.com 或 @zedsaid。