How display and edit HTML in iOS

北城以北 提交于 2019-12-24 07:40:36

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9840574/how-display-and-edit-html-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!