Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using iOS 5 rich text editor

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

like image 270
iOS.Lover Avatar asked Nov 04 '11 16:11

iOS.Lover


People also ask

How do I open RTF files on my iPhone?

Office File Formats Microsoft Office and Apple Pages documents are all supported natively, as are TXT, RTF, and PDF; when you receive one, just tap it to open it. If you want to edit these files, there are some options.

Does iOS have a text editor?

The most obvious alternative is Apple's own Pages, which is free and can not only edit simple text files, but is also a fully-featured word processor that can create complex documents. Don't forget Apple's Notes either, as this is essentially a replacement for TextEdit and it's available on every Mac, iPad and iPhone.

What is the best code editor for iOS?

Textastic is probably the most popular code editor for iOS, and for good reasons. It supports syntax highlighting of more than 80 programming and markup languages. Its interface is snappy and uses the native iOS framework Core Text. You can access files via FTP, SFTP and WebDAV or from Dropbox or Google Drive.


1 Answers

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.

like image 85
Mike Mertsock Avatar answered Sep 18 '22 05:09

Mike Mertsock