Yuwen Yan
Yuwen Yan

Reputation: 4935

How display and edit HTML in iOS

With UIWebView, I can display HTML very well. With UITextView, I can edit HTML. With what can I both display and edit HTML?

Is there any open source project or tutorial available?

Upvotes: 1

Views: 3622

Answers (3)

Abdul Karim
Abdul Karim

Reputation: 4523

check this lib which is updated and can be used above ios 11 and written in swift

-AztecEditor link

-updated version of
RichEditorView link

Upvotes: -1

Sam
Sam

Reputation: 27354

You can show your html inside a UIWebView with a WYSIWYG editor inside. There are a lot of WYSIWYG editors out there. I like CKEditor (demo).

See the Developer Documentation - Integration for how to set / get the text of the WYSIWYG editor.

How to Create the WYSIWYG Editor:

  • Download the script files and bundle them in your app
  • Include the script (from ckeditor website)
<head>
...
    <script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
</head>
  • Add text area with your initial text
<textarea id="editor1" name="editor1">
        <p>Initial value.</p>
</textarea>
  • Create editor using javascript
<script type="text/javascript">   
     CKEDITOR.replace( 'editor1' );
</script>

How to get Text from WYSIWYG Editor

Use the script:

var editor_data = CKEDITOR.instances.editor1.getData();

You can use UIWebView's stringByEvaluatingJavaScriptFromString: method to run javascript code to get / set the html content as needed.

Upvotes: 3

Ian L
Ian L

Reputation: 5591

Have you tried using the UITextViewDelegate method (such as textViewDidChange:) and reload the content of the UITextView's text property back into the UIWebView (using loadHTMLString:baseURL:)?

- (void)textViewDidChange:(UITextView *)textView {
    [self.webView loadHTMLString:textView.text baseURL:nil];
}

This will allow you to manually amend HTML and see the result.

Upvotes: 0

Related Questions