1.自定義Label的UIMenuController
#import "ZZYMenuLabel.h"
@implementation ZZYMenuLabel
/**
* xib創(chuàng)建label時調(diào)用
*/
- (void)awakeFromNib
{
[self setUp];
}
/**
* 代碼創(chuàng)建label時調(diào)用
*/
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setUp];
}
return self;
}
- (void)setUp
{
self.userInteractionEnabled = YES;
[self addGestureRecognizer:[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress)]];
}
- (void)longPress
{
NSLog(@"%s",__func__);
//1.設(shè)置label為第一響應(yīng)者
//通過設(shè)置第一響應(yīng)者UIMenuController可以獲得支持哪些操作的信息,操作怎么處理
[self becomeFirstResponder];
//2.設(shè)置UIMenuController
UIMenuController * menu = [UIMenuController sharedMenuController];
//當長按label的時候蕴忆,這個方法會不斷調(diào)用,menu就會出現(xiàn)一閃一閃不斷顯示狼荞,需要在此處進行判斷
if (menu.isMenuVisible)return;
//自定義 UIMenuController
UIMenuItem * item1 = [[UIMenuItem alloc]initWithTitle:@"剪切" action:@selector(myCut:)];
UIMenuItem * item2 = [[UIMenuItem alloc]initWithTitle:@"粘貼" action:@selector(myPaste:)];
menu.menuItems = @[item1,item2];
[menu setTargetRect:self.bounds inView:self];
// [menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
#pragma mark - 對控件權(quán)限進行設(shè)置
/**
* 設(shè)置label可以成為第一響應(yīng)者
*
* @注意:不是每個控件都有資格成為第一響應(yīng)者
*/
- (BOOL)canBecomeFirstResponder
{
return YES;
}
/**
* 設(shè)置label能夠執(zhí)行那些具體操作
*
* @param action 具體操作
*
* @return YES:支持該操作
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
// NSLog(@"%@",NSStringFromSelector(action));
if(action == @selector(cut:) || action == @selector(copy:) || action == @selector(myCut:)|| action == @selector(myPaste:)) return YES;
return NO;
}
#pragma mark - 方法的實現(xiàn)
//- (void)cut:(id)sender
//{
//
// NSLog(@"%@",sender);
//
//}
- (void)myCut:(UIMenuController *) menu
{
NSLog(@"%s---%@",__func__,menu);
//復制文字到剪切板
[self copy:menu];
//清空文字
self.text = nil;
}
- (void)cut:(UIMenuController *)menu
{
//復制文字到剪切板
[self copy:menu];
//清空文字
self.text = nil;
}
- (void)copy:(UIMenuController *)menu
{
//當沒有文字的時候調(diào)用這個方法會崩潰
if (!self.text) return;
//復制文字到剪切板
UIPasteboard * paste = [UIPasteboard generalPasteboard];
paste.string = self.text;
}
- (void)myPaste:(UIMenuController *)menu
{
//將剪切板文字賦值給label
UIPasteboard * paste = [UIPasteboard generalPasteboard];
self.text = paste.string;
}
@end
2.UITableViewCell的使用
#import "ZZYTableViewCell.h"
@implementation ZZYTableViewCell
- (BOOL)canBecomeFirstResponder
{
return YES;
}
@end
#import "ZZYTableViewController.h"
#import "ZZYTableViewCell.h"
@interface ZZYTableViewController ()
@property (nonatomic, weak) ZZYTableViewCell * selectCell;
@end
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//當menucontroller顯示派哲,點擊不同的cell時為什么會顯示霸株。
// menuController的顯示依賴于第一響應(yīng)者,當點擊另外的cell時去件,當前cell取消第一響應(yīng)者狀態(tài)坡椒,menucontroller自動消失
UIMenuController * menu = [UIMenuController sharedMenuController];
NSLog(@"%d",menu.isMenuVisible);
//防止點擊多次創(chuàng)建
if (menu.isMenuVisible)
{
[menu setMenuVisible:NO animated:YES];
}
else
{
ZZYTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
self.selectCell = cell;
[cell becomeFirstResponder];
UIMenuItem * item0 = [[UIMenuItem alloc]initWithTitle:@"分享" action:@selector(share:)];
UIMenuItem * item1 = [[UIMenuItem alloc]initWithTitle:@"評論" action:@selector(comment:)];
UIMenuItem * item2 = [[UIMenuItem alloc]initWithTitle:@"點贊" action:@selector(praise:)];
menu.menuItems = @[item0,item1,item2];
[menu setTargetRect:CGRectMake(0, cell.frame.size.height * 0.5, cell.frame.size.width, cell.frame.size.height) inView:cell];
[menu setMenuVisible:YES animated:YES];
}
}
- (void)share:(UIMenuController *)menu
{
NSLog(@"%@",self.selectCell.textLabel.text);
}
- (void)comment:(UIMenuController *)menu
{
}
- (void)praise:(UIMenuController *)menu
{
}
//防止拖動tableView時產(chǎn)生的BUG
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
UIMenuController * menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:YES];
}
3.設(shè)置位置
/**
* 設(shè)置menu顯示的位置信息
*
* @param targetRect menu需要顯示的矩形區(qū)域
* @param targetView targetRect會以targetView的左上角為坐標原點進行顯示
*/
- (void)setTargetRect:(CGRect)targetRect inView:(UIView *)targetView;