IOS开发之文本复制
2013-08-19 11:03
260 查看
原始地址:IOS开发之文本复制
本文基于富文本:DTCoreText
给要复制的视图添加长按事件:
重写下面方法:
本文基于富文本:DTCoreText
#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 *htmlString = [[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:@"此处是富文本,其他同理"]; }
相关文章推荐
- iOS开发中可复制文本的Label
- iOS开发怎样实现全系统的文本复制粘贴
- IOS开发之文本复制
- iOS开发-复制文本到剪切板
- iOS开发—使用TYAttributedLabel实现字体大小颜色各异并且带链接的文本
- iOS开发——超链接富文本
- iOS开发:自定义带下划线文本的UIButton
- iOS开发必知Swift结构体中实现写时复制的方法
- 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词
- iOS开发- OC之富文本
- iOS开发_剪贴板操作_复制粘贴功能
- iOS--开发文本输入框遇到的一些问题
- iOS App开发中扩展RCLabel组件进行基于HTML的文本布局
- ios开发文字排版,段落排版,富文本
- iOS/Objective-C开发 字典NSDictionary的深复制(使用category)
- iOS开发~禁用文本输入框拷贝和粘贴等功能
- ios开发之tableview的编辑模式下的Edit Done Delete按钮文本
- UITextField 文本字段控件-IOS开发
- ios开发--图文混排(富文本)
- iOS开发_iPhone 实现剪贴板操作_iPhone 复制粘贴功能