Created
February 24, 2014 00:40
-
-
Save elnaqah/9179668 to your computer and use it in GitHub Desktop.
load rtf file in UItextView
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 characters
NSURL *rtfPath = [[NSBundle mainBundle] URLForResource: @"description_ar" withExtension:@"rtf"]; | |
NSAttributedString *attributedStringWithRtf = [[NSAttributedString alloc] initWithFileURL:rtfPath options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil]; | |
self.textView.attributedText=attributedStringWithRtf; |
Updated Swift version:
if let rtfPath = NSBundle.mainBundle().URLForResource("filename", withExtension: "rtf") {
do {
let attributedStringWithRtf = try NSAttributedString(URL: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
textView.attributedText = attributedStringWithRtf
} catch {
print("No rtf content found!")
}
}
Updates for Swift 3.0
if let rtfPath = Bundle.main.url(forResource: "filename", withExtension: "rtf") {
do {
let attributedStringWithRtf = try NSAttributedString(url: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil)
textView.attributedText = attributedStringWithRtf
} catch {
print("No rtf content found!")
}
}
Good code. worked for me but I am unable to save images with NSTextAttachment
. Please help.
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get the following error for the above swift code: