How to show an HTML string on a UILabel in iOS?

前端 未结 7 1918
心在旅途
心在旅途 2020-12-12 18:25

I am getting a title in HTML format as

Example

I

相关标签:
7条回答
  • 2020-12-12 18:31

    I did this on UITextView as follows:

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

    Or you can use RTLabel library: https://github.com/honcheng/RTLabel to display html text along with its formatting on a label.

    0 讨论(0)
  • 2020-12-12 18:36

    Swift 4+

    For Swift 4 and above use:

    guard let data = "foo".data(using: String.Encoding.unicode) else { return }
    
    try? titleLabel.attributedText = 
       NSAttributedString(data: data,
                       options: [.documentType:NSAttributedString.DocumentType.html], 
            documentAttributes: nil)
    
    0 讨论(0)
  • 2020-12-12 18:39

    For iOS7 or more you can use this:

    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;
    
    0 讨论(0)
  • 2020-12-12 18:39
    -(NSString*)convertHtmlPlainText:(NSString*)HTMLString{
        NSData *HTMLData = [HTMLString dataUsingEncoding:NSUTF8StringEncoding];
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:HTMLData options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:NULL error:NULL];
        NSString *plainString = attrString.string;
    
        return plainString;
    }
    
    UILabel * htmlLabel = [UILabel alloc] init];
    htmlLabel.text = [self convertHtmlPlainText:htmlResponse];
    
    0 讨论(0)
  • 2020-12-12 18:45

    If Html Text is like

    var planeText = "Unseen and not Purchased!"
    //setting color to the text
    var htmlText = "<font color=\"#f20707\">" + planeText + "</font>"  
    

    We can set the text to UILabel like this

    if let htmlData = htmlText.data(using: String.Encoding.unicode) {
        do {
             let attributedText = try NSAttributedString(data: htmlData,
                 options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], 
                 documentAttributes: nil)   
             //Setting htmltext to uilable 
             uilable.attributedText = attributedText
    
           } catch let e as NSError {
             //setting plane text to uilable cause of err
             uilable.text = planeText
             print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
           }
    }
    
    0 讨论(0)
  • 2020-12-12 18:52

    Extension for Swift 4+

    extension UILabel {
      func set(html: String) {
        if let htmlData = html.data(using: .unicode) {
          do {
            self.attributedText = try NSAttributedString(data: htmlData,
                                                         options: [.documentType: NSAttributedString.DocumentType.html],
                                                         documentAttributes: nil)
          } catch let e as NSError {
            print("Couldn't parse \(html): \(e.localizedDescription)")
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题