MathJax在UIWebView中移除所有的body内容

MathJax removes all body content in UIWebView

本文关键字:body 内容 UIWebView MathJax      更新时间:2023-09-26

我试图使一个类方法,将采取一个MathJax字符串,包装所需的HTML周围,并将其加载到指定的UIWebView。我的代码如下:

+ (void)loadMathJax:(NSString *)jax inWebView:(UIWebView *)webView fontSize:(int)fontSize
{
    NSString *mathJaxPath = [[NSBundle mainBundle] pathForResource:@"MathJax" ofType:@"js" inDirectory:@"MathJax"];
    NSString *htmlContent = [NSString stringWithFormat:@""
                             @"<html><head>"
                             @"<meta name='viewport' content='initial-scale=1.0' />"
                             @"<style type='text/css'>"
                             @"body { background-color:transparent; font-size:%d; color: black; }"
                             @"</style>"
                             @"<script type='text/javascript' src='%@?config=TeX-AMS-MML_HTMLorMML-full'>"
                             @"</head>"
                             @"<body>''(%@'')</body>"
                             @"</html>"
                             , fontSize, mathJaxPath, jax];
    NSLog(@"%@",htmlContent);
   [webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:mathJaxPath]];
}

如果我使用这种方法,NSLog显示正确的HTML(例如<body>'(Z_1 = 4 + 2i')</body>),但模拟器中的web视图是空白的。使用Safari网页检查器显示,<body>标签之间的所有内容都已被删除。

我尝试过的事情:

  • 使用MathJax CDN而不是本地加载。没有区别。
  • ''(%@'')替换为test text(即删除MathJax标签)。app运行时<body>标签之间仍然没有内容
  • 删除MathJax <script>调用。文本现在显示,但不渲染为数学。
  • baseURL:从mathJaxPath更改为nil没有区别。

算出来了。我忘记了MathJax导入的关闭</script>标记。