Copy Nsattributedstring in Uipasteboard

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



Leave a reply



Submit