2

我正在学习格式化可编辑的 html 文本并在 UIWebView 中显示

//html文件

<html>
<body>
    <div id="content" contenteditable="true" style="font-family: Helvetica">

    </div>
</body>
</html>

//加载到WebView

[self.webView loadHTMLString:[[NSString alloc] initWithData:
                    [[NSFileHandle fileHandleForReadingAtPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]] readDataToEndOfFile] encoding:NSUTF8StringEncoding]

                     baseURL:[NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]]
 ];

像bi这样的一些标签可以通过

-(IBAction)bold:(UIButton *)sender {
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.execCommand('bold')"];
}

-(IBAction)italic:(UIButton *)sender {
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.execCommand('italic')"];
}

我想找到一个命令将当前行文本格式化为h1h2 .... ppre标记。

我搜索了很多但仍然卡住了。请给个建议!

4

1 回答 1

0

我从这个文件中得到答案

[self.webView stringByEvaluatingJavaScriptFromString:@"document.execCommand('formatBlock', false, 'p')"];

[self.webView stringByEvaluatingJavaScriptFromString:@"document.execCommand('formatBlock', false, 'h1')"];
于 2013-08-12T11:33:22.840 回答