Skip to content

Instantly share code, notes, and snippets.

@mltbnz
Last active August 30, 2017 15:56
Show Gist options
  • Save mltbnz/d4a271a216d5008fd434a3b47defb672 to your computer and use it in GitHub Desktop.
Save mltbnz/d4a271a216d5008fd434a3b47defb672 to your computer and use it in GitHub Desktop.
Create an attributed String from a RTF File.
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