你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS開發之文本復制

IOS開發之文本復制

編輯:關於IOS

 

本文基於富文本: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:@"此處是富文本,其他同理"];
}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved