Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

iphone - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...