我有一组问题存储在 SQLite 数据库中。对于数学科目,这些内容以 html 格式存储为公式/方程。我检索问题并将其存储为字符串:
NSString *htmlstring = @"If 3<em>x</em><sup><strong>2</strong></sup> + 11<em>x</em> + 6 $$\\ge$$ 0, then <em>x</em> $$\\in $$";
要在 UILabel 中显示此字符串,我使用以下代码:
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
questionLbl.attributedText = attrStr;
此代码未按预期工作,输出为:
预期输出是:
在从数据库获取数据或将 html 转换为纯文本时,我不明白哪里出了问题。
更新
从下面 Ashmi 的回答中,我发现文本是 latex 格式的。 现在我们如何将latex命令转换为符号是一个问题。有3种可能:
- 将 Latex 转换为 HTML(按照 Ashmi 的建议),然后加载该 HTML 在标签中。
- 将 Latex 转换为 unicode 字符以直接加载到标签中。
- 使用库(如果有),可能类似于 Math.h
最佳答案
请检查下面的代码,您的 HTML 格式可能不正确,所以我只是更改 HTML 格式并获得您想要的输出。有关在 HTML 中制作数学符号的更多信息,请查看下面的链接。
https://www.univie.ac.at/moe/formeln.html
NSString *htmlstring = @"If 3<em>x</em><sup><strong>2</strong></sup> + 11<em>x</em> + 6 ≥0, then <em>x</em>€";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlstring dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSLog(@"%@",attrStr.string);
关于ios - 将包含 Latex Command for Mathematical 公式的 HTML 文本转换为输出正确公式的纯文本或属性文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38825030/