Last active
August 30, 2017 15:56
-
-
Save mltbnz/d4a271a216d5008fd434a3b47defb672 to your computer and use it in GitHub Desktop.
Create an attributed String from a RTF File.
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
import Foundation | |
import UIKit | |
extension NSAttributedString { | |
convenience init?(fromRTF name: String) { | |
guard let url = Bundle.main.url(forResource: name, withExtension: "rtf") else { | |
return nil | |
} | |
guard let data = try? Data(contentsOf: url) else { | |
return nil | |
} | |
let options: [String: Any] = [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType, | |
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue] | |
try? self.init(data: data, | |
options: options, | |
documentAttributes: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment