UIMenuController
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIMenuController相关的知识,希望对你有一定的参考价值。
UIMenuController的示例
UIMenuController须知
- 默认情况下, 有以下控件已经支持UIMenuController
- UITextField
- UITextView
- UIWebView
让其他控件也支持UIMenuController(比如UILabel)
- 自定义UILabel
- 重写2个方法
```objc /* * 让label有资格成为第一响应者/ - (BOOL)canBecomeFirstResponder { return YES; }
/* * label能执行哪些操作(比如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;
} ```
- 实现各种操作方法
```objc - (void)cut:(UIMenuController *)menu { // 将自己的文字复制到粘贴板 [self copy:menu];
// 清空文字
self.text = nil;
}
- (void)copy:(UIMenuController )menu { // 将自己的文字复制到粘贴板 UIPasteboard board = [UIPasteboard generalPasteboard]; board.string = self.text; }
- (void)paste:(UIMenuController )menu { // 将粘贴板的文字复制到自己身上 UIPasteboard board = [UIPasteboard generalPasteboard]; self.text = board.string; } ```
- 让label成为第一响应者
objc // 这里的self是label [self becomeFirstResponder];
- 显示UIMenuController
objc UIMenuController *menu = [UIMenuController sharedMenuController]; // targetRect: MenuController需要指向的矩形框 // targetView: targetRect会以targetView的左上角为坐标原点 [menu setTargetRect:self.bounds inView:self]; // [menu setTargetRect:self.frame inView:self.superview]; [menu setMenuVisible:YES animated:YES];
自定义UIMenuController内部的Item
- 添加item
objc // 添加MenuItem(点击item, 默认会调用控制器的方法) UIMenuItem *ding = [[UIMenuItem alloc] initWithTitle:@"顶" action:@selector(ding:)]; UIMenuItem *replay = [[UIMenuItem alloc] initWithTitle:@"回复" action:@selector(replay:)]; UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(report:)]; menu.menuItems = @[ding, replay, report];
代码示例
一、让label有menu,需要自定义label
- (void)awakeFromNib
{
[self setup];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup ];
}
return self;
}
- (void)setup
{
//label一定要可以交互
self.userInteractionEnabled = YES;
[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)]];
}
/**
* 让label有资格成为第一响应者
*/
- (BOOL)canBecomeFirstResponder
{
return YES;
}
/**
* label能执行哪个操作(比如copy、paste)等
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
//只返回YES支持所有操作
if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:)) {
return YES;
}
// 自定义menu的时候,这里放回NO就行了
return NO;
}
/**
* 剪切方法
*/
- (void)cut:(UIMenuController *)menu
{
//将文字保存到粘贴板
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
pasteBoard.string = self.text;
//清空label的文字
self.text = nil;
}
- (void)copy:(UIMenuController *)menu
{
//将文字保存到粘贴板
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
pasteBoard.string = self.text;
}
// 粘贴
- (void)paste:(UIMenuController *)menu
{
//将文字保存到粘贴板
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
self.text = pasteBoard.string;
}
- (void)click
{
//1、让label成为第一响应者
[self becomeFirstResponder];
//2、显示MenuController
UIMenuController *menu = [UIMenuController sharedMenuController];
//menu围着哪个区域显示,显示上下左右由系统决定
//[menu setTargetRect:self.frame inView:self.superview];
//或者
[menu setTargetRect:self.bounds inView:self];
//自定义menu
//显示menu菜单
[menu setMenuVisible:YES animated:YES];
}
二、自定义menu
UILabel *secondLabel = [[UILabel alloc] init];
secondLabel.frame = CGRectMake(100, 200, 100, 50);
secondLabel.backgroundColor = [UIColor yellowColor];
[secondLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPP)]];
secondLabel.text = @"ppppppp";
secondLabel.userInteractionEnabled = YES;
self.label = customLabel;
[self.view addSubview:secondLabel];
self.label = secondLabel;
- (void)clickPP
{
//1、让label成为第一响应者
[self becomeFirstResponder];
//2、显示MenuController
UIMenuController *menu = [UIMenuController sharedMenuController];
//menu围着哪个区域显示,显示上下左右由系统决定
//[menu setTargetRect:self.frame inView:self.superview];
//或者
[menu setTargetRect:self.label.bounds inView:self.label];
//自定义menu
//自定义menu的按钮方法必须放在控制器中
UIMenuItem *ding = [[UIMenuItem alloc] initWithTitle:@"顶" action:@selector(ding:)];
UIMenuItem *replay = [[UIMenuItem alloc] initWithTitle:@"回复" action:@selector(replay:)];
UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(report:)];
menu.menuItems = @[ding,replay,report];
//显示menu菜单
[menu setMenuVisible:YES animated:YES];
}
/**
* 让label有资格成为第一响应者
*/
- (BOOL)canBecomeFirstResponder
{
return YES;
}
/**
* 顶
*/
- (void)ding:(UIMenuController *)meun
{
NSLog(@"ding");
}
/**
* 顶
*/
- (void)replay:(UIMenuController *)meun
{
NSLog(@"replay");
}
/**
* 顶
*/
- (void)report:(UIMenuController *)meun
{
NSLog(@"report");
}
以上是关于UIMenuController的主要内容,如果未能解决你的问题,请参考以下文章