下面的代碼爲我工作:
//Function below concatenates an HTML file String, stores it locally, then calls it to display in a UIWebView using NSURLRequest with MathJax typesetting:
func formatWebViewText(){
//Specify the text that will go in the HTML file:
let htmlString: NSString = "<head><script type=\"text/x-mathjax-config\">MathJax.Hub.Config({tex2jax: {inlineMath: [ ['$','$'] ],},\"HTML-CSS\": { linebreaks: { automatic: true, width: \"container\" } } })</script><script src='" +
String(NSBundle.mainBundle().pathForResource("MathJax", ofType: "js", inDirectory: "MathJax-masterSlim")!) + //reference to local MathJax files
"?config=TeX-AMS-MML_HTMLorMML'></script></head>" +
"<body>" + questionMessage + "</body></html>" // "questionMessage" contains the text to be typeset/processed by MathJax
//Give HTML file a name:
let htmlFileName: NSString = "htmlTempFile.html"
//Create a reference to the temporary directory that will store the HTML file:
let tempDirectory: NSString = NSTemporaryDirectory()
//Append reference temporary directory to include HTML file name:
let htmlFilePath: NSString = tempDirectory.stringByAppendingPathComponent(htmlFileName as String)
//Convert appended temporary directory NSString to an NSURL object:
htmlUrl = NSURL(fileURLWithPath: htmlFilePath as String)
//Below, HTML string is written to a file:
do { try htmlString.writeToFile(htmlFilePath as String, atomically: true, encoding: NSUTF8StringEncoding) }
catch { }
//HTML file URL is fetched and displayed in UIWebView:
QuestionWebView.loadRequest(NSURLRequest(URL: htmlUrl!))
}
如何在Webview中打印mathjax垂直中心 – srinadh