This article is based on Rich Text: DTCoreText
[Cpp]
# Import <Foundation/Foundation. h>
# Import <MobileCoreServices/UTCoreTypes. h> // Add this framework
@ Interface UIPasteboard (AttributedString)
-(Void) setAttributedString :( NSAttributedString *) attributedString;
@ End
# Import <Foundation/Foundation. h>
# Import <MobileCoreServices/UTCoreTypes. h> // Add this framework
@ Interface UIPasteboard (AttributedString)
-(Void) setAttributedString :( NSAttributedString *) attributedString;
@ End
[Cpp]
# Import "UIPasteboard + AttributedString. h"
@ Implementation UIPasteboard (AttributedString)
-(Void) setAttributedString :( NSAttributedString *) attributedString
{
// \ Ufffc is an object placeholder. The purpose is to copy only text information when there is an image in Rich Text !!!
NSString * htmlString = [[attributedString string] stringByReplacingOccurrencesOfString: @ "\ ufffc" withString: @ ""];
NSMutableDictionary * item = [NSMutableDictionary dictionaryWithCapacity: 1];
[Item setValue: htmlString forKey :( NSString *) kUTTypeText];
Self. items = [NSArray arrayWithObject: item];
}
@ End
# Import "UIPasteboard + AttributedString. h"
@ Implementation UIPasteboard (AttributedString)
-(Void) setAttributedString :( NSAttributedString *) attributedString
{
// \ Ufffc is an object placeholder. The purpose is to copy only text information when there is an image in Rich Text !!!
NSString * htmlString = [[attributedString string] stringByReplacingOccurrencesOfString: @ "\ 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:
[Cpp]
UILongPressGestureRecognizer * gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longTap :)];
[Self. selectedBackgroundView addGestureRecognizer: gestureRecognizer];
GestureRecognizer. minimumPressDuration = 1.0;
UILongPressGestureRecognizer * gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longTap :)];
[Self. selectedBackgroundView addGestureRecognizer: gestureRecognizer];
GestureRecognizer. minimumPressDuration = 1.0;
[Cpp]
-(Void) longTap :( UILongPressGestureRecognizer *) ges
{
[Self becomeFirstResponder];
UIMenuController * menu = [UIMenuController sharedMenuController];
// Size and position
[Menu setTargetRect: [self frame] inView: self. superView];
[Menu setMenuVisible: YES animated: YES];
}
-(Void) longTap :( UILongPressGestureRecognizer *) ges
{
[Self becomeFirstResponder];
UIMenuController * menu = [UIMenuController sharedMenuController];
// Size and position
[Menu setTargetRect: [self frame] inView: self. superView];
[Menu setMenuVisible: YES animated: YES];
}
Rewrite the following method:
[Cpp]
// Whether to intercept Event Response
-(BOOL) canBecomeFirstResponder
{
Return YES;
}
// What kind of operation will be responded
-(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"];
}
// Whether to intercept Event Response
-(BOOL) canBecomeFirstResponder
{
Return YES;
}
// What kind of operation will be responded
-(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"];
}