xcode 使用 iOS 5 富文本编辑器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8012245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:20:16  来源:igfitidea点击:

Using iOS 5 rich text editor

iphoneiosxcodeipad

提问by Mc.Lover

as you know the Mail app in iOS 5 have a rich text editor is there any possible way to use this feature for a regular UITextView ?

如您所知,iOS 5 中的邮件应用程序有一个富文本编辑器,有没有办法将此功能用于常规 UITextView ?

enter image description here

enter image description here

采纳答案by Greg

The iOS 5 rich text edit control is also present in the notes app in iOS 4 (make a rich text note on the computer and sync it to see).

iOS 5 的富文本编辑控件也出现在 iOS 4 的笔记应用中(在电脑上做一个富文本笔记,同步查看)。

This is a custom Apple-made control which they use in their own apps, but it is not published in any official developer API. It's probably in the SDK somewhere, but because it is undocumented, even if you find it and use it, Apple will reject your app.

这是他们在自己的应用程序中使用的 Apple 定制控件,但未在任何官方开发人员 API 中发布。它可能在 SDK 的某个地方,但因为它没有记录,即使你找到并使用它,Apple 也会拒绝你的应用程序。

Basically, if you want a rich text control you will have to make your own.

基本上,如果您想要富文本控件,则必须自己制作。

Edit:Try using this: https://github.com/omnigroup/OmniGroup/tree/master/Frameworks/OmniUI/iPad/Examples/TextEditor/. I haven't used it, so I don't know how well it will work. (Link from this question)

编辑:尝试使用这个:https: //github.com/omnigroup/OmniGroup/tree/master/Frameworks/OmniUI/iPad/Examples/TextEditor/。我没用过,不知道效果如何。(来自这个问题的链接)

回答by Mike Mertsock

I know of two fundamental approaches to creating a rich text editor in iOS 5:

我知道在 iOS 5 中创建富文本编辑器的两种基本方法:

  1. Use Core Text and a custom view. I don't have any experience with this approach.

  2. Use a UIWebView (instead of a UITextView) and the contentEditableHTML attribute. The basic idea is to load a custom HTML document from your app resources directory. The bare minimum that it needs is this:

    <div contentEditable>TEXT_PLACEHOLDER</div>

  1. 使用核心文本和自定义视图。我对这种方法没有任何经验。

  2. 使用 UIWebView(而不是 UITextView)和contentEditableHTML 属性。基本思想是从您的应用程序资源目录加载自定义 HTML 文档。它需要的最低限度是:

    <div contentEditable>TEXT_PLACEHOLDER</div>

To initialize the rich text editor view: 1. Load the contents of this file into an NSMutableStringand replace the TEXT_PLACEHOLDER string with the text you want to edit. 2. Send the loadHTMLString:baseURL:message to the UIWebView with that HTML string.

初始化富文本编辑器视图: 1. 将此文件的内容加载到一个中,NSMutableString并用您要编辑的文本替换 TEXT_PLACEHOLDER 字符串。2.loadHTMLString:baseURL:使用该 HTML 字符串将消息发送到 UIWebView。

Now you have a UIWebView displaying your text inside a divwith contentEditable. At this point, you should be able to run your app tap on the text, and be presented with a cursor and be able to add/remove text. The next step is to add rich text formatting functionality. This is done with a set of simple javascript function calls. See the Mozilla documentation on contentEditablefor a great reference. You will also want to add a Javascript function to your HTML template file like this:

现在你有一个 UIWebView 在一个divwith 中显示你的文本contentEditable。此时,您应该能够在文本上运行您的应用程序,并显示光标并能够添加/删除文本。下一步是添加富文本格式功能。这是通过一组简单的 javascript 函数调用完成的。请参阅关于 contentEditable 的 Mozilla 文档以获得很好的参考。您还需要向 HTML 模板文件中添加一个 Javascript 函数,如下所示:

function getHtmlContent() { return document.getElementById('myDiv').innerHTML; }

So you can easily retrieve the edited text as HTML using [myWebView stringByEvaluatingJavaScriptFromString:@"getHtmlContent()"]. You can also add custom context menu items like you show in the screen shot in your question.

因此,您可以使用[myWebView stringByEvaluatingJavaScriptFromString:@"getHtmlContent()"]. 您还可以添加自定义上下文菜单项,就像您在问题的屏幕截图中显示的那样。

If you have access to the Apple iOS dev center, the WWDC session Rich Text Editing in Safari on iOStalks all about this approach.

如果您有权访问 Apple iOS 开发中心,那么 WWDC 会话在 iOS 上的 Safari 中的富文本编辑会讨论所有这种方法。

A variation of this approach is to use a third-party rich text editor like TinyMCE. I've heard of some success with integrating this into a UIWebView in iOS 5. Again this relies on the contentEditable attribute.

这种方法的一个变体是使用第三方富文本编辑器,如 TinyMCE。我听说在 iOS 5 中将其集成到 UIWebView 中取得了一些成功。这再次依赖于 contentEditable 属性。

回答by aryaxt

Here is my implementation. Still haven't added UIMenuController functionality, but it's planned to be added soon.

这是我的实现。还没有添加 UIMenuController 功能,但计划很快添加。

https://github.com/aryaxt/iOS-Rich-Text-Editor

https://github.com/aryaxt/iOS-Rich-Text-Editor

回答by easypayroll

look at https://github.com/gfthr/FastTextViewwhich is more good open source editor than Omni editor

看看https://github.com/gfthr/FastTextView比 Omni 编辑器更好的开源编辑器