Last active
March 9, 2017 21:10
-
-
Save iamjason/504b79fd9a6efdc7a23e75e494738d67 to your computer and use it in GitHub Desktop.
Revisions
-
iamjason revised this gist
Mar 9, 2017 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,9 +2,11 @@ import UIKit public extension String { static let htmlBase = "<html><head><style type='text/css'> body { color: %COLOR%; } %STYLE% </style></head><body>%BODY%</body></html>" static let cssBase = " h1 { font-family: 'GTWalsheimLight'; font-size: 34px; } h2 { font-family: 'GTWalsheimBold'; font-size: 24px; } h3 { font-family:'GTWalsheimLight'; font-size: 18px; } h4 { font-family:'GTWalsheimBold'; font-size: 14px; } h5 { font-family:'GTWalsheimBold'; font-size: 12px; } p { font-family: 'SourceSansPro-Regular'; font-size: 16px; } p.strong { font-family: 'SourceSansPro-Semibold'; font-size: 16px; }" public func HTMLtoAttributedText(_ color:UIColor = UIColor.black, css:String = String.cssBase) -> NSAttributedString? { // maybe break this out into a string extension, but // for now... lets just leave it here @@ -21,6 +23,7 @@ public extension String { let combined = (String.htmlBase as NSString) .replacingOccurrences(of: "%BODY%", with: self) .replacingOccurrences(of: "%COLOR%", with: hexString(color) ) .replacingOccurrences(of: "%STYLE%", with: css) if let data = combined.data(using: .utf8) { do { -
iamjason created this gist
Mar 9, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ import UIKit public extension String { static let htmlBase = "<html><head><style type='text/css'> body { color: %COLOR%; } h1 { font-family: 'GTWalsheimLight'; font-size: 34px; } h2 { font-family: 'GTWalsheimBold'; font-size: 24px; } h3 { font-family:'GTWalsheimLight'; font-size: 18px; } h4 { font-family:'GTWalsheimBold'; font-size: 14px; } h5 { font-family:'GTWalsheimBold'; font-size: 12px; } p { font-family: 'SourceSansPro-Regular'; font-size: 16px; } p.strong { font-family: 'SourceSansPro-Semibold'; font-size: 16px; } </style></head><body>%BODY%</body></html>" public func HTMLtoAttributedText(_ color:UIColor = UIColor.black) -> NSAttributedString? { // maybe break this out into a string extension, but // for now... lets just leave it here func hexString(_ c:UIColor) -> String { var r:CGFloat = 0 var g:CGFloat = 0 var b:CGFloat = 0 var a:CGFloat = 0 c.getRed(&r, green: &g, blue: &b, alpha: &a) let rgb:Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0 return String(format:"#%06x", rgb) } let combined = (String.htmlBase as NSString) .replacingOccurrences(of: "%BODY%", with: self) .replacingOccurrences(of: "%COLOR%", with: hexString(color) ) if let data = combined.data(using: .utf8) { do { return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil) } catch { print("error creating attributed string") } } return nil } }