首先說一說UIMenuController
- 默認(rèn)情況下, 有以下控件已經(jīng)支持UIMenuController
- UITextField
- UITextView
- UIWebView
也可以 讓其他控件也支持UIMenuController(比如UILabel)
/**
* 讓label有資格成為第一響應(yīng)者
*/
- (BOOL)canBecomeFirstResponder
{
return YES;
}
/**
* label能執(zhí)行哪些操作(比如copy, paste等等)
* @return YES:支持這種操作
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:)) return YES;
return NO;
}
//給label添加點(diǎn)擊手勢
- (void)setGestureRecognizer
{
self.userInteractionEnabled = YES;
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)]];
}
- (void)labelClick
{
// 1.label要成為第一響應(yīng)者(作用是:告訴UIMenuController支持哪些操作, 這些操作如何處理)
[self becomeFirstResponder];
// 2.顯示MenuController
UIMenuController *menu = [UIMenuController sharedMenuController];
// targetRect: MenuController需要指向的矩形框
// targetView: targetRect會以targetView的左上角為坐標(biāo)原點(diǎn)
[menu setTargetRect:self.bounds inView:self];
// [menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
- (void)cut:(UIMenuController *)menu
{
// 將自己的文字復(fù)制到粘貼板
[self copy:menu];
// 清空文字
self.text = nil;
}
- (void)copy:(UIMenuController *)menu
{
// 將自己的文字復(fù)制到粘貼板
UIPasteboard *board = [UIPasteboard generalPasteboard];
board.string = self.text;
}
- (void)paste:(UIMenuController *)menu
{
// 將粘貼板的文字 復(fù)制 到自己身上
UIPasteboard *board = [UIPasteboard generalPasteboard];
self.text = board.string;
}
自定義UIMenuController內(nèi)部的Item
// 添加MenuItem(點(diǎn)擊item, 默認(rèn)會調(diào)用控制器的方法)
UIMenuItem *spot = [[UIMenuItem alloc] initWithTitle:@"支持" action:@selector(ding:)];
UIMenuItem *replay = [[UIMenuItem alloc] initWithTitle:@"回復(fù)" action:@selector(replay:)];
UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"舉報" action:@selector(report:)];
menu.menuItems = @[spot, replay, report];
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者