I am getting a HTML Response from a webservice Below is the HTML I am getting in response
TopicGud mrng.
\\n
From: https://stackoverflow.com/a/5581178/237838
To convert HTML to plain text Download File
and use
stringByConvertingHTMLToPlainText
function on your NSString
OR
You can use DTCoreText (previously known as NSAttributedString Additions for HTML).
The above answer in Swift 3:
var str = "<html> ... some html ... </html>"
let htmlStringData = NSString(string: str).data(using: String.Encoding.utf8.rawValue)
let html = htmlStringData
do {
let htmlAttrString = try? NSAttributedString(
data: html!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil
)
agreementText.attributedText = htmlAttrString
} catch {
print("An error occured")
}
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()
Sometimes we need to display HTML content on the screen by using UILabel. How to display the HTML content in UILabel we see that in this article. let’s start and achieve it.
Objective C:
NSString * htmlString = @"<html><body> <b> HTML in UILabel is here…. </b> </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString
dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel * yourLabel = [[UILabel alloc] init];
yourLabel.attributedText = attrStr;
Swift:
var htmlString = “<html><body> <b> HTML in UILabel is here…. </b> </body></html>”
var attrStr: NSAttributedString? = nil
do {
if let data = htmlString.data(using: .unicode) {
attrStr = try NSAttributedString(data: data, options: [
NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html.rawValue
], documentAttributes: nil)
}
} catch {
}
var yourLabel = UILabel()
yourLabel.attributedText = attrStr
Ref: https://medium.com/@javedmultani16/html-text-in-uilabel-ios-f1e0760bcac5
Lately I've been dealing with partial HTML snippets and converting them to attributed strings with the ability to add attributes. Here is my version of the extension
import Foundation
import UIKit
extension String {
func htmlAttributedString(attributes: [String : Any]? = .none) -> NSAttributedString? {
guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return .none }
guard let html = try? NSMutableAttributedString(
data: data,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: .none) else { return .none }
html.setAttributes(attributes, range: NSRange(0..<html.length))
return html
}
}
I call it thus:
let attributes = [
NSForegroundColorAttributeName: UIColor.lightGray,
NSFontAttributeName : UIFont.systemFont(ofSize: 12).traits(traits: .traitItalic)
]
label?.attributedText = partialHTMLString.htmlAttributedString(attributes: attributes)
Use RTLabel library to convert the HTML text. I have used it several times. It works. Here is link to the library and a sample code.
https://github.com/honcheng/RTLabel.
Hope I helped.