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 .... pまたはpreタグにフォーマットするコマンドを見つけたいと思います。

私はよく検索しますが、まだ立ち往生しています。提案してください!

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 に答える