分享

IOS开发之文本复制

 没原创_去搜索 2015-07-23


本文基于富文本:DTCoreText


[cpp] 
#import <Foundation/Foundation.h>  
#import <MobileCoreServices/UTCoreTypes.h>//添加此框架  
 
@interface UIPasteboard (AttributedString) 
- (void) setAttributedString:(NSAttributedString *)attributedString; 
@end 

#import <Foundation/Foundation.h>
#import <MobileCoreServices/UTCoreTypes.h>//添加此框架

@interface UIPasteboard (AttributedString)
- (void) setAttributedString:(NSAttributedString *)attributedString;
@end


[cpp]
#import "UIPasteboard+AttributedString.h"  
 
@implementation UIPasteboard (AttributedString) 
- (void) setAttributedString:(NSAttributedString *)attributedString 

    //\ufffc为对象占位符,目的是当富文本中有图像时,只复制文本信息!!!  
    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为对象占位符,目的是当富文本中有图像时,只复制文本信息!!!
 NSString *htmlString = [[attributedString string] stringByReplacingOccurrencesOfString:@"\ufffc" withString:@""];
 NSMutableDictionary *item = [NSMutableDictionary dictionaryWithCapacity:1];
 [item setValue:htmlString forKey:(NSString *)kUTTypeText];
 self.items = [NSArray arrayWithObject:item];
}
@end


给要复制的视图添加长按事件:


[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]; 
        //尺寸和添加到哪里  
    [menu setTargetRect: [self frame] inView: self.superView]; 
    [menu setMenuVisible: YES animated: YES]; 

- (void)longTap:(UILongPressGestureRecognizer *)ges
{
 [self becomeFirstResponder];
 UIMenuController * menu = [UIMenuController sharedMenuController];
        //尺寸和添加到哪里
 [menu setTargetRect: [self frame] inView: self.superView];
 [menu setMenuVisible: YES animated: YES];
}

重写下面方法:


[cpp]
//是否截获事件响应  
- (BOOL)canBecomeFirstResponder 

    return YES; 

 
//什么样的操作会被响应  
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 

    return action == @selector(copy:); 

 
- (void)copy:(id)sender 

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; 
    [pasteboard setAttributedString:@"此处是富文本,其他同理"]; 

//是否截获事件响应
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

//什么样的操作会被响应
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
 return action == @selector(copy:);
}

- (void)copy:(id)sender
{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setAttributedString:@"此处是富文本,其他同理"];
}

 


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多