Mansuu....
Mansuu....

Reputation: 1226

How to show html text in UILabel in iOS- swift 3.0

I want to show data coming from server in UICollectionView. the data is coming in html string like given below

  <span> recognized <a 

href='#/userProfile/NZ==/XQ=='> Punit Kumar </a> for Delivers on time</span>

I want it to be shown like this

enter image description here

how can I achieve that? Someone please help me.

Upvotes: 2

Views: 8209

Answers (5)

Gourav Mandliya
Gourav Mandliya

Reputation: 85

Get html attributed string with font parameter from string

extension String
    {
            func getHTMLFromString(htmlText: String,fonthexColor:String,fontName:String,fontSize:Int)->NSAttributedString?
            {
                  let modifiedFont = String(format:"<span style=\"color:\(fonthexColor);font-family:\(fontName); font-size: \(fontSize)\">%@</span>", htmlText)
        
                  let attrStr = try! NSAttributedString(
                      data: modifiedFont.data(using: .unicode, allowLossyConversion: true)!,
                      options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue],
                      documentAttributes: nil)
             
                  return attrStr
              }
        }

Upvotes: 0

Sazzadhusen Iproliya
Sazzadhusen Iproliya

Reputation: 892

Swift 5

let htmlText = "<html><body> Some html string </body></html>"

guard let data = dicData.desc.data(using: String.Encoding.unicode) else { return }

let attributedText = try! NSAttributedString(data: data, options: [.documentType:NSAttributedString.DocumentType.html], documentAttributes: nil)

Upvotes: 1

Paul B
Paul B

Reputation: 5117

Swift 4

Update for this answer

    extension NSAttributedString {
        internal convenience init?(html: String) {
            guard let data = html.data(using: String.Encoding.utf16, allowLossyConversion: false) else {
                // not sure which is more reliable: String.Encoding.utf16 or String.Encoding.unicode
                return nil
            }
            guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
                return nil
            }
            self.init(attributedString: attributedString)
        }
    }

    label.attributedText = NSAttributedString(html: "<span> recognized <a href='#/userProfile/NZ==/XQ=='> Punit Kumar </a> for Delivers on time</span>")

Upvotes: 7

David Pasztor
David Pasztor

Reputation: 54706

You need to convert your HTML formatted string into NSAttributedString, then set that attributed string as the text of your label. You can convert your HTML formatted string to NSAttributedString using the below extension

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

You can use above extension like this: let attributedString = NSAttributedString(html: ""<html><body> Some html string </body></html>"")

Then you can set the attributed string as the text of your UILabel using myLabel.attributedText = attributedString

Upvotes: 2

Allan
Allan

Reputation: 2086

Maybe like this

Swift

let htmlText = "<p>etc</p>"

if let htmlData = htmlText.data(using: String.Encoding.unicode) {
    do {
        let attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
    } catch let e as NSError {
        print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
    }
}

Objective-C

 NSString * htmlString = @"<html><body> Some html string </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

UILabel * myLabel = [[UILabel alloc] init];
myLabel.attributedText = attrStr;

WebView

[detailView loadHTMLString:[NSString stringWithFormat:@"<div style='text-align:justify; font-size:13px;font-family:HelveticaNeue;color:#362932;'>%@",[model.dict valueForKey:@"description"]] baseURL:nil];

Upvotes: 0

Related Questions