Using iOS 5 rich text editor

2020-02-02 11:36发布

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 ?

enter image description here

4条回答
Luminary・发光体
2楼-- · 2020-02-02 11:39

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

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

查看更多
狗以群分
3楼-- · 2020-02-02 11:48

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).

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.

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)

查看更多
SAY GOODBYE
4楼-- · 2020-02-02 11:51

I know of two fundamental approaches to creating a rich text editor in 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 contentEditable HTML 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>

To initialize the rich text editor view: 1. Load the contents of this file into an NSMutableString and 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.

Now you have a UIWebView displaying your text inside a div with 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 contentEditable for a great reference. You will also want to add a Javascript function to your HTML template file like this:

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.

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

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.

查看更多
够拽才男人
5楼-- · 2020-02-02 12:03

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

查看更多
登录 后发表回答