ios - 将包含 Latex Command for Mathematical 公式的 HTML 文本转换为输出正确公式的纯文本或属性文本

标签 ios objective-c sqlite nsattributedstring mathematical-expressions

我有一组问题存储在 SQLite 数据库中。对于数学科目,这些内容以 html 格式存储为公式/方程。我检索问题并将其存储为字符串:

NSString *htmlstring = @"If 3<em>x</em><sup><strong>2</strong></sup>&nbsp;+ 11<em>x</em>&nbsp;+ 6 $$\\ge$$&nbsp;0, then <em>x</em>&nbsp;$$\\in $$";

要在 UILabel 中显示此字符串,我使用以下代码:

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        questionLbl.attributedText = attrStr;

此代码未按预期工作,输出为:

enter image description here

预期输出是:

enter image description here

在从数据库获取数据或将 html 转换为纯文本时,我不明白哪里出了问题。

更新

从下面 Ashmi 的回答中,我发现文本是 latex 格式的。 现在我们如何将latex命令转换为符号是一个问题。有3种可能:

  1. 将 Latex 转换为 HTML(按照 Ashmi 的建议),然后加载该 HTML 在标签中。
  2. 将 Latex 转换为 unicode 字符以直接加载到标签中。
  3. 使用库(如果有),可能类似于 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>&nbsp;+ 11<em>x</em>&nbsp;+ 6 &#x2265;0, then <em>x</em>&#x20AC";

  NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlstring dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

  NSLog(@"%@",attrStr.string);

enter image description here

关于ios - 将包含 Latex Command for Mathematical 公式的 HTML 文本转换为输出正确公式的纯文本或属性文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38825030/

相关文章:

ios - App Store 发表评论/评价 iOS 8 Swift

ios - 私有(private)协议(protocol)实现?

ios - 从 swift viewcontroller 调用 .c 文件中的 swift 函数

ruby-on-rails - ActiveRecord::Base.connection.execute 重复 sql 输出

ios - 如何调用 sqlite3_errmsg 以了解 sqlite3_prepare_v2 失败的原因

ios - 将UIView/图形上下文划分为多个区域

ios - 不在 iOS 8 中从 Facebook 获取用户的电子邮件个人信息

ios - 关闭模态视图时不调用 ViewDidAppear

ios - UITableViewDelegate 在 iOS 中是否有类似 tableViewWillEndDisplayingCell 的方法?

sqlite - 我可以/应该针对 SQLite 数据库运行我的网站吗?