Skip to content

Instantly share code, notes, and snippets.

@soggybag
Created October 17, 2015 06:16
Show Gist options
  • Save soggybag/6aaba6cbbb73c2fef801 to your computer and use it in GitHub Desktop.
Save soggybag/6aaba6cbbb73c2fef801 to your computer and use it in GitHub Desktop.
Swift Attributes String Extension
extension String {
func stringToAttributedString() -> NSAttributedString {
var html = self
while let range = html.rangeOfString("\n") {
html.replaceRange(range, with: "</br>")
}
html = "<span style='font-family: Helvetica; font-size:14pt'>"+html+"</span>"
let data = html.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)
let attrStr = try! NSAttributedString(data: data!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
return attrStr
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment