Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display HTML text in UILabel iphone

I am getting a HTML Response from a webservice Below is the HTML I am getting in response

<p><strong>Topic</strong>Gud mrng.</p> \n<p><strong>Hello Everybody</strong>: How are you.</p> \n<p><strong>I am fine</strong>: 1 what about you.</p> 

I need to display the text in UILabel.

Please help

like image 817
Khushboo Avatar asked Apr 08 '13 06:04

Khushboo


People also ask

How do you display HTML formatted text in UILabel Swift?

To render this text properly in UILabel or UITextView, you need to convert it to NSAttributedString . NSAttributedString has built-in support for this conversion. First, we need to convert HTML string to Data . let htmlString = "This is a <b>bold</b> text."

How do I change my UILabel text?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.


2 Answers

You can do it without any third-party libraries by using attributed text. I believe it does accept HTML fragments, like the one you're getting, but you may want to wrap it in a complete HTML document so that you can specify CSS:

static NSString *html =     @"<html>"      "  <head>"      "    <style type='text/css'>"      "      body { font: 16pt 'Gill Sans'; color: #1a004b; }"      "      i { color: #822; }"      "    </style>"      "  </head>"      "  <body>Here is some <i>formatting!</i></body>"      "</html>";  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 200)]; NSError *err = nil; label.attributedText =     [[NSAttributedString alloc]               initWithData: [html dataUsingEncoding:NSUTF8StringEncoding]                    options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }         documentAttributes: nil                      error: &err]; if(err)     NSLog(@"Unable to parse label text: %@", err); 

Not concise, but you can mop up the mess by adding a category to UILabel:

@implementation UILabel (Html)  - (void) setHtml: (NSString*) html     {     NSError *err = nil;     self.attributedText =         [[NSAttributedString alloc]                   initWithData: [html dataUsingEncoding:NSUTF8StringEncoding]                        options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }             documentAttributes: nil                          error: &err];     if(err)         NSLog(@"Unable to parse label text: %@", err);     }  @end 

[someLabel setHtml:@"Be <b>bold!</b>"]; 
like image 62
Paul Cantrell Avatar answered Sep 22 '22 23:09

Paul Cantrell


Swift 4: version

extension String {     func htmlAttributedString() -> NSAttributedString? {         guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }         guard let html = try? NSMutableAttributedString(             data: data,             options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],             documentAttributes: nil) else { return nil }         return html     } } 

Swift 3: version

extension String { func htmlAttributedString() -> NSAttributedString? {     guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }     guard let html = try? NSMutableAttributedString(         data: data,         options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],         documentAttributes: nil) else { return nil }     return html     } } 

Swift 2: version

extension String {         func htmlAttributedString() -> NSAttributedString? {             guard let data = self.dataUsingEncoding(NSUTF16StringEncoding, allowLossyConversion: false) else { return nil }             guard let html = try? NSMutableAttributedString(               data: data,                options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],                documentAttributes: nil) else { return nil }             return html         } } 

use it as:

label.attributedText = yourStringVar.htmlAttributedString() 
like image 38
Oliver White Avatar answered Sep 23 '22 23:09

Oliver White