Display html text in uitextview

How to display HTML text in text form?

For example,

string <h1>Krupal testing <span style="font-weight: bold;">Customer WYWO</span></h1> 

Suppose the text is in bold, so it appears in text as a bold line, but I want to display plain text. Is this possible in the iPhone SDK?

+77
ios iphone uitextview
Mar 16 '10 at 11:57
source share
10 answers

Use UIWebView on iOS 5-.

In iOS 6+, you can use UITextView.attributedString , see https://stackoverflow.com/a/167448/ for.




There is also an undocumented method -[UITextView setContentToHTMLString:] . Do not use this if you want to ship to the AppStore.

+52
Mar 16 '10 at 12:02
source share

Use the following code block for ios 7+.

 NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src='http://blogs.babble.com/famecrawler/files/2010/11/mickey_mouse-1097.jpg' width=70 height=100 />"; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: nil ]; textView.attributedText = attributedString; 
+229
Jan 08
source share

For Swift 4:

 let htmlString = "<html>" + "<head>" + "<style>" + "body {" + "background-color: rgb(230, 230, 230);" + "font-family: 'Arial';" + "text-decoration:none;" + "}" + "</style>" + "</head>" + "<body>" + "<h1>A title</h1>" + "<p>A paragraph</p>" + "<b>bold text</b>" + "</body></head></html>" let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue) let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html] let attributedString = try! NSAttributedString(data: htmlData!, options: options, documentAttributes: nil) textView.attributedText = attributedString 

For Swift 3 :

 let htmlString = "<html>" + "<head>" + "<style>" + "body {" + "background-color: rgb(230, 230, 230);" + "font-family: 'Arial';" + "text-decoration:none;" + "}" + "</style>" + "</head>" + "<body>" + "<h1>A title</h1>" + "<p>A paragraph</p>" + "<b>bold text</b>" + "</body></head></html>" let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue) let attributedString = try! NSAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) textView.attributedText = attributedString 
+38
Nov 29 '16 at 20:44
source share

Bhup's

The answer is correct, but if you want to combine your own font with UILabel or UITextView with HTML content, you need to adjust your html a bit:

 NSString *htmlString = @"<b>Bold</b><br><i>Italic</i><p> <del>Deleted</del><p>List<ul><li>Coffee</li><li type='square'>Tea</li></ul><br><a href='URL'>Link </a>"; htmlString = [htmlString stringByAppendingString:@"<style>body{font-family:'YOUR_FONT_HERE'; font-size:'SIZE';}</style>"]; /*Example: htmlString = [htmlString stringByAppendingString:[NSString stringWithFormat:@"<style>body{font-family: '%@'; font-size:%fpx;}</style>",_myLabel.font.fontName,_myLabel.font.pointSize]]; */ NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: nil ]; textView.attributedText = attributedString; 

U can see the difference in the picture below: enter image description here

+16
May 23 '16 at 4:16
source share

You can look at the OHAttributedLabel classes, I used them to overcome this problem with textField. At the same time, they redefined the drawRect method to obtain the desired style.

https://github.com/AliSoftware/OHAttributedLabel

+12
Sep 16 '11 at 11:27
source share

My first answer was before iOS 7 introduced explicit support for displaying attribute strings in common controls. Now you can set the attributedText of UITextView to an NSAttributedString created from HTML content using:

 -(id)initWithData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error 

- initWithData: options: documentAttributes: error: (Apple Doc)

Original answer saved for history:

If you are not using UIWebView , your solution will rely directly on CoreText . As ElanthiraiyanS notes, some open source projects have appeared to simplify the processing of rich text. I would recommend NSAttributedString-Additions-For-HTML ( Edit: The project was superseded by DTCoreText ), which has classes for generating and displaying attribute strings from HTML.

+9
Sep 20 '11 at 19:33
source share

The answer came up to me from BHUPI.

Code transfer is quick, as shown below:

Note "allowLossyConversion: false"

if you set to true, it will display clear text.

 let theString = "<h1>H1 title</h1><b>Logo</b><img src='http://www.aver.com/Images/Shared/logo-color.png'><br>~end~" let theAttributedString = try! NSAttributedString(data: theString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) UITextView_Message.attributedText = theAttributedString 
+4
Oct 07 '15 at 9:50
source share

For Swift3

  let theString = "<h1>H1 title</h1><b>Logo</b><img src='http://www.aver.com/Images/Shared/logo-color.png'><br>~end~" let theAttributedString = try! NSAttributedString(data: theString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) UITextView_Message.attributedText = theAttributedString 
+3
Jun 14 '17 at 8:41
source share

You can also use another way. The Three20 library offers a method by which we can create text text. You can get the library here: http://github.com/facebook/three20/

The TTStyledTextLabel class has a textFromXHTML method: I assume this will serve the purpose. But that would be possible in readonly mode. I do not think that this will allow writing or editing HTML content.

There is also a question that can help you with this: HTML string content for UILabel and TextView

I hope this will be helpful.

-2
Sep 21 2018-11-11T00:
source share

NSDoc saves the text file in a line to an html file, and then loads it into a web view, which is in the same place as your UITextView ..

-3
Sep 21 '11 at 20:17
source share



All Articles