IOS開發(fā)之文本復(fù)制

本文轉(zhuǎn)載自紅黑聯(lián)盟

#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為對象占位符,目的是當(dāng)富文本中有圖像時,只復(fù)制文本信息!!!
      NSString *String = [[attributedString string] stringByReplacingOccurrencesOfString:@"\ufffc" withString:@""];
      NSMutableDictionary *item = [NSMutableDictionary dictionaryWithCapacity:1]; 
     [item setValue:htmlString forKey:(NSString *)kUTTypeText];
      self.items = [NSArray arrayWithObject:item]; 
}
@end 

給要復(fù)制的視圖添加長按事件:

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

重寫下面方法:

//是否截獲事件響應(yīng) 
 - (BOOL)canBecomeFirstResponder {
     return YES; 
} 
//什么樣的操作會被響應(yīng)  
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
     return action == @selector(copy:); 
}
- (void)copy:(id)sender {
     UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
     [pasteboard setAttributedString:@"此處是富文本,其他同理"]; 
} 

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,116評論 25 708
  • 在iOS開發(fā)中經(jīng)常會涉及到觸摸事件。本想自己總結(jié)一下,但是遇到了這篇文章,感覺總結(jié)的已經(jīng)很到位,特此轉(zhuǎn)載。作者:L...
    WQ_UESTC閱讀 6,106評論 4 26
  • 好奇觸摸事件是如何從屏幕轉(zhuǎn)移到APP內(nèi)的?困惑于Cell怎么突然不能點擊了?糾結(jié)于如何實現(xiàn)這個奇葩響應(yīng)需求?亦或是...
    Lotheve閱讀 57,893評論 51 603
  • 翻譯自“Collection View Programming Guide for iOS” 0 關(guān)于iOS集合視...
    lakerszhy閱讀 3,919評論 1 22
  • 畢業(yè)一年多,做過一份工作,時長1年零三個月。 結(jié)束上一份工作到現(xiàn)在再次找到工作間隔5個月。 當(dāng)初辭職后就一直忙著找...
    44e387f08401閱讀 324評論 0 0