问题
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