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.
Asked
Active
Viewed 724 times
-5

koen
- 5,383
- 7
- 50
- 89

saikumar Lakshmi Pallagani
- 146
- 1
- 8
-
2Questions 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
-
1Why 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 Answers
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
-
1
-
Now i have given more explanation, can you please look in to it – saikumar Lakshmi Pallagani Aug 08 '18 at 14:09
-
If you need HTML to show in a View somehwere then UIWebView is the right way to go, you cant upload image so we see whats your real problem can you ? – Aragunz Aug 08 '18 at 14:17
-
1
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