Text replication in IOS development

Source: Internet
Author: User

 


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"];
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.