Created
October 17, 2015 06:16
-
-
Save soggybag/6aaba6cbbb73c2fef801 to your computer and use it in GitHub Desktop.
Swift Attributes String Extension
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
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