iOS 中有三個控制項天生支援剪下、複製、貼上:UITextField、UITextView 與 UIWebView。在文字上長按即可喚出系統的編輯選單。
UITextField 的文字操作:

UITextView 的文字操作:

UIWebView 的文字操作:

使用者複製或剪下的內容,實際上被放進了系統剪貼板。剪貼板不只承載字串,也能放圖片與 URL,因此開發者可以直接用它做 App 內、甚至 App 間的資料傳遞。
三個取得剪貼板的方法,分別對應三種作用範圍:
// 系統級剪貼板,全裝置共用
+ (UIPasteboard *)generalPasteboard;
// 以名稱取得或建立自訂剪貼板;create 為 NO 且剪貼板不存在時回傳 nil
+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName
create:(BOOL)create;
// 取得一個僅供當前 App 使用的具名剪貼板
+ (UIPasteboard *)pasteboardWithUniqueName;
系統級剪貼板的資料在 App 被刪除後仍然存在;具名剪貼板則可在自家 App 之間共享資料;第三種等價於以 nil 名稱呼叫第二種方法,通常只用於 App 內部。要注意的是,以這種方式建立的剪貼板預設不持久化,App 結束後內容就會消失,若需要保留必須把 persistent 設為 YES。
常用的名稱、刪除與狀態屬性:
@property (readonly, nonatomic) NSString *name; // 剪貼板名稱 + (void)removePasteboardWithName:(NSString *)pasteboardName; // 依名稱刪除 @property (getter=isPersistent, nonatomic) BOOL persistent; // 是否持久化 @property (readonly, nonatomic) NSInteger changeCount; // 變更次數(系統剪貼板需重開機才歸零)
針對「最新一組資料」的存取:
- (NSArray<NSString *> *)pasteboardTypes; // 型別清單 - (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes; // 是否含指定型別 - (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType; // 取出 NSData - (nullable id)valueForPasteboardType:(NSString *)pasteboardType; // 取出物件值 - (void)setValue:(id)value forPasteboardType:(NSString *)pasteboardType; // 寫入物件值 - (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType; // 寫入 NSData
支援多組資料(items)的存取:
@property (readonly, nonatomic) NSInteger numberOfItems; // 資料組數
- (nullable NSArray *)pasteboardTypesForItemSet:(nullable NSIndexSet *)itemSet;
- (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes
inItemSet:(nullable NSIndexSet *)itemSet;
- (nullable NSIndexSet *)itemSetWithPasteboardTypes:(NSArray *)pasteboardTypes;
- (nullable NSArray *)valuesForPasteboardType:(NSString *)pasteboardType
inItemSet:(nullable NSIndexSet *)itemSet;
- (nullable NSArray *)dataForPasteboardType:(NSString *)pasteboardType
inItemSet:(nullable NSIndexSet *)itemSet;
@property (nonatomic, copy) NSArray *items; // 全部資料組
- (void)addItems:(NSArray<NSDictionary<NSString *, id> *> *)items; // 追加一組
上述方法都要傳入型別識別字串,系統預先定義了幾組常用常數:
UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListString; // 純文字 UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListURL; // URL UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListImage; // 圖片 UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListColor; // 顏色
相較之下,下面這組「直接對應型別」的屬性更簡潔,日常開發幾乎都用它們:
@property (nullable, nonatomic, copy) NSString *string; @property (nullable, nonatomic, copy) NSArray<NSString *> *strings; @property (nullable, nonatomic, copy) NSURL *URL; @property (nullable, nonatomic, copy) NSArray<NSURL *> *URLs; @property (nullable, nonatomic, copy) UIImage *image; @property (nullable, nonatomic, copy) NSArray<UIImage *> *images; @property (nullable, nonatomic, copy) UIColor *color; @property (nullable, nonatomic, copy) NSArray<UIColor *> *colors;
剪貼板變動會伴隨通知,可用來監看外部 App 寫入的內容:
UIKIT_EXTERN NSString *const UIPasteboardChangedNotification; // 內容變更 UIKIT_EXTERN NSString *const UIPasteboardChangedTypesAddedKey; // 新增的型別 UIKIT_EXTERN NSString *const UIPasteboardChangedTypesRemovedKey; // 移除的型別 UIKIT_EXTERN NSString *const UIPasteboardRemovedNotification; // 剪貼板被刪除
#import "CopyView.h"
@interface CopyView ()
@property (strong, nonatomic) UIImageView *img1;
@property (strong, nonatomic) UIImageView *img2;
@end
@implementation CopyView
-(UIImageView *)img1 {
if (_img1 == nil) {
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, 100.0f)];
NSString *path = [[NSBundle mainBundle] pathForResource:@"NetworldImage" ofType:@"jpg"];
_img1.image = [UIImage imageWithContentsOfFile:path];
}
return _img1;
}
-(UIImageView *)img2 {
if (_img2 == nil) {
_img2 = [[UIImageView alloc] initWithFrame:
CGRectMake(CGRectGetMaxX(self.img1.frame) + 50.0f, 20.0f, 100.0f, 100.0f)];
_img2.backgroundColor = [UIColor lightGrayColor];
}
return _img2;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
[self addSubview:self.img1];
[self addSubview:self.img2];
}
return self;
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
NSArray *methodNameArr = @[@"copy:", @"cut:", @"select:", @"selectAll:", @"paste:"];
if ([methodNameArr containsObject:NSStringFromSelector(action)]) {
return YES;
}
return [super canPerformAction:action withSender:sender];
}
// 把 img1 寫入系統剪貼板
-(void)copy:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setImage:self.img1.image];
}
// 從剪貼板讀回圖片並顯示在 img2
-(void)paste:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
self.img2.image = [pasteboard image];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self becomeFirstResponder];
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:self.img1.frame inView:self];
[menuController setMenuVisible:YES animated:YES];
}
@end
在控制器中掛上這個 view:
#import "ViewController.h"
#import "CopyView.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CopyView *cv = [[CopyView alloc] initWithFrame:self.view.bounds];
self.view = cv;
}
@end

