Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save elenipapanik/551874070f2940f0ac72e394a19b6752 to your computer and use it in GitHub Desktop.
Save elenipapanik/551874070f2940f0ac72e394a19b6752 to your computer and use it in GitHub Desktop.
func stringByStrippingHTMLElementsNativeWay() -> String {
let encodedData = self.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
var attributedString: NSAttributedString?
do {
attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
} catch {
print("Error while trying to create html attributed string: \(error)")
return ""
}
let trimmedStringFromNewLine = attributedString!.string.stringByReplacingOccurrencesOfString("\n", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
let trimmedStringFromWhiteSpaces = trimmedStringFromNewLine.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
return trimmedStringFromWhiteSpaces
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment