Created
July 4, 2016 15:03
-
-
Save elenipapanik/551874070f2940f0ac72e394a19b6752 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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