IOS开发之文本复制


#import <Foundation/Foundation.h>  
#import <MobileCoreServices/UTCoreTypes.h>//添加此框架   
@interface UIPasteboard (AttributedString) 
- (void) setAttributedString:(NSAttributedString *)attributedString;
@end
#import "UIPasteboard+AttributedString.h"
@implementation UIPasteboard (AttributedString) 
- (void) setAttributedString:(NSAttributedString *)attributedString {     //\ufffc为对象占位符,目的是当富文本中有图像时,只复制文本信息!!!
      NSString *String = [[attributedString string] stringByReplacingOccurrencesOfString:@"\ufffc" withString:@""];
      NSMutableDictionary *item = [NSMutableDictionary dictionaryWithCapacity:1]; 
     [item setValue:htmlString forKey:(NSString *)kUTTypeText];
      self.items = [NSArray arrayWithObject:item]; 
}
@end

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

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

重写下面方法:

//是否截获事件响应 
 - (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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值