Putting NSMutableAttributedString with font and size into UIPasteboard to paste into another app
Import
#import <MobileCoreServices/UTCoreTypes.h>
Copy NSAttributedString
in ios
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
NSData *rtf = [attributedString dataFromRange:NSMakeRange(0, attributedString.length)
documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType}
error:nil];
if (rtf) {
[item setObject:rtf forKey:(id)kUTTypeFlatRTFD];
}
[item setObject:attributedString.string forKey:(id)kUTTypeUTF8PlainText];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.items = @[item];
Paste NSAttributedString
in ios
NSAttributedString *attributedString;
NSData* rtfData = [[UIPasteboard generalPasteboard] dataForPasteboardType:(id)kUTTypeFlatRTFD];
if (rtfData) {
attributedString = [[NSAttributedString alloc] initWithData:rtfData options:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
}
_lblResult.attributedText=attributedString;
i hope this will help you
Copy NSAttributedString to pasteboard
You want either NSRTFPboardType or NSRTFDPboardType along with the NSAttributedString's RTFFromRange:documentAttributes:
/RTFDFromRange:documentAttributes:
and setData
on the pasteboard.
Copy Text with Formatting - iOS 6 NSAttributedString to Pasteboard
The following will work for whatever textView is currently first responder:
[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
Since my copy button is only accessible when my textView had resigned firstResponder, I had to do a little more:
[self.noteTextView select:self];
self.noteTextView.selectedRange = NSMakeRange(0, [self.noteTextView.text length]);
[[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
[self.noteTextView resignFirstResponder];
Thanks to the following posts which pointed me in the right direction:
Perform copy/cut from UIResponderStandardEditActions
can I call a select method from UIResponderStandardEditActions?
Related Topics
Uncaught Exception: This Class Is Not Key Value Coding-Compliant
Why Is -Diddeselectrowatindexpath Not Being Called
Uicollectionview Spacing Margins
Add a Uiview Above All, Even the Navigation Bar
Error: Error Domain=Nsurlerrordomain Code=-1001 "The Request Timed Out."
Restricting App Installations from Appstore Only to Users with iPhone 5/5S/5C
Swift 3: Type 'Any' Has No Subscript Members
How to Debug an iOS Extension (.Appex)
Synchronous Url Request on Swift 2
How to Run Nstimer in Background Beyond 180Sec in iOS 7
How to Add a Button to the Mkpointannotation
How to Make View the Size of Another View in Swiftui
How to Attach Debugger to iOS App After Launch
How to Get Location User with Cllocationmanager in Swift
Send Messages Between iOS and Watchos with Watchconnectivity in Watchos2