Original address: Text replication for iOS development
This article is based on Rich Text: dtcoretext
# Import <Foundation/Foundation. h> # import <mobilecoreservices/utcoretypes. h> // Add this framework @ interface uipasteboard (attributedstring)-(void) setattributedstring :( nsattributedstring *) attributedstring; @ end
# Import "uipasteboard + attributedstring. H "@ implementation uipasteboard (attributedstring)-(void) setattributedstring :( nsattributedstring *) attributedstring {// \ ufffc is the object placeholder, so that when rich text contains images, only Copy text information !!! Nsstring * htmlstring = [[attributedstring string] identifier: @ "\ ufffc" withstring: @ "]; nsmutabledictionary * Item = [nsmutabledictionary dictionarywithcapacity: 1]; [item setvalue: htmlstring forkey :( nsstring *) kuttypetext]; self. items = [nsarray arraywithobject: item];} @ end
Add a long-press event to the view to be copied:
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];[self.selectedBackgroundView addGestureRecognizer:gestureRecognizer];gestureRecognizer.minimumPressDuration = 1.0;
-(Void) longtap :( uilongpressgesturerecognizer *) GES {[self becomefirstresponder]; uimenucontroller * menu = [uimenucontroller sharedmenucontroller]; // dimensions and add location [menu settargetrect: [self frame] inview: Self. superview]; [menu setmenuvisible: Yes animated: Yes];}
Rewrite the following method:
// Whether to intercept Event Response-(bool) canbecomefirstresponder {return yes;} // what kind of action will be returned-(bool) can1_maction :( SEL) Action withsender :( ID) sender {Return Action ==@ selector (copy :);}-(void) Copy :( ID) sender {uipasteboard * pasteboard = [uipasteboard generalpasteboard]; [pasteboard setattributedstring: @ "Rich Text here, similar to others"];}