1

I have set of questions stored in SQLite database. For mathematics subject those are stored as formula/equations in html format. I retrieve question and stored it as string:

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 $$";

To display this string in UILabel, I use following piece of code :

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

This code not worked as expected and output is:

enter image description here

The expected output is:

enter image description here

I didn't understand where its going wrong, while fetching data from database or converting html to plain text.

UPDATE

From Ashmi's answer below I figured out the text is in Latex format. How can we convert latex command to symbol is now a question. There are 3 possibilities:

  1. Convert Latex to HTML(as suggested by Ashmi), then load that HTML in label.
  2. Convert Latex to unicode character to load directly in label.
  3. Using library if any, may be like Math.h
iAkshay
  • 1,143
  • 1
  • 13
  • 35

1 Answers1

3

Please check below code your HTML format may be not correct So I just change HTML format and get output you want. For more information to make Math symbol in HTML please check below link.

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

ashmi123
  • 710
  • 1
  • 6
  • 21
  • I'm getting html string from database dynamically. However, link provided by you help me to figure out the text is in LaTeX format. So,need to find out a way to get symbol from LaTex command in iOS. – iAkshay Aug 08 '16 at 10:42
  • Yes,But you can search out for the html to LaTex format and pass it to string. – ashmi123 Aug 08 '16 at 10:44