-5

Up to now i can show some HTML tags like "p", "li" on UILabel, but I want to display "td", "tr" tags also. Is it possible? I know we can display in WKWebView but for dynamic size in table view i want to put a label kind of UI, is there any third party for it please let me know.

koen
  • 5,383
  • 7
  • 50
  • 89
  • 2
    Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow – Ashley Mills Aug 08 '18 at 13:57
  • 1
    Why not put the `WKWebView` inside your cell? It certainly has been done before, see eg here: https://stackoverflow.com/questions/48463531 – koen Aug 08 '18 at 15:18
  • @saikumar, please check the answer as below: https://stackoverflow.com/a/75004837/5581345 – Arshad Shaik Jan 04 '23 at 11:38

3 Answers3

0
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;
Aragunz
  • 511
  • 5
  • 18
0

UILabel is not suitable to display Table or Image

We can use TextView or Webview.

If you want to get height in tableViewCell, you can use textView.

To get attributed String from Html, code as below:

extension String {
    var htmlToAttributedString: NSAttributedString? {
        guard let data = data(using: .utf8) else { return nil }
        do {
            return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            return nil
        }
    }
    
    var htmlToString: String {
            return htmlToAttributedString?.string ?? ""
        }
}

display html string in textview as below:

cell.txtViewHTML.attributedText = htmlString.htmlToAttributedString

To get height of String, code as below:

extension String {
    func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil)
        return ceil(boundingBox.height)
    }
    
    func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil)
        return ceil(boundingBox.width)
    }
}

To get height of AttributedString, code as below:

extension NSAttributedString {
    func height(withConstrainedWidth width: CGFloat) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, context: nil)
        return ceil(boundingBox.height)
    }
    
    func width(withConstrainedHeight height: CGFloat) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, context: nil)
        return ceil(boundingBox.width)
    }
}

Get the height of html string as below:

let htmlStringHeight = htmlString.htmlToAttributedString!.height(withConstrainedWidth: screenWidth - 50) + CGFloat(tblViewDefaultHeight)

let screenSize = UIScreen.main.bounds

let screenWidth = screenSize.width

-50, is textview leading and trailing distance in the cell

tblViewDefaultHeight ie., if you want to add some extra spacing

Arshad Shaik
  • 1,095
  • 12
  • 18
-1

In general, you need an HTML parser to parse the HTML. UILabel does not provide this functionality

Adam
  • 1,054
  • 1
  • 12
  • 26