标签:添加 情况 with ini selector dshow bubuko fir hide
@property (weak, nonatomic) IBOutlet UILabel *label;
- (void)viewDidLoad {
[super viewDidLoad];
// 允许 Label 交互
self.label.userInteractionEnabled = YES;
// 给 Label 添加手势
[self.label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)]];
}
#pragma mark - 监听手势点击事件
- (void)labelClick {
// 让 label 成为第一响应者
[self.label becomeFirstResponder];
// 创建菜单
UIMenuController *menu = [UIMenuController sharedMenuController];
// 设置菜单内容,自定义菜单项
menu.menuItems = @[
[[UIMenuItem alloc] initWithTitle:@"顶" action:@selector(ding:)],
[[UIMenuItem alloc] initWithTitle:@"回复" action:@selector(reply:)],
[[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(warn:)]
];
// 设置菜单箭头的方向
menu.arrowDirection = UIMenuControllerArrowUp;
// 菜单最终显示的位置
[menu setTargetRect:self.label.bounds inView:self.label];
// 显示菜单
[menu setMenuVisible:YES animated:YES];
}
#pragma mark - UIMenuController 相关
// 让 Label 具备成为第一响应者的资格,UIResponder 方法
- (BOOL)canBecomeFirstResponder {
return YES;
}
// 通过第一响应者的这个方法告诉 UIMenuController 可以显示什么内容,UIResponder 方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if ((action == @selector(copy:) && self.label.text) || // 需要有文字才能支持复制,系统菜单项
(action == @selector(cut:) && self.label.text) || // 需要有文字才能支持剪切
action == @selector(paste:) ||
action == @selector(ding:) || // 自定义菜单项
action == @selector(reply:) ||
action == @selector(warn:))
{
return YES;
}
return NO;
}
#pragma mark - 监听 MenuItem 的点击事件
- (void)cut:(UIMenuController *)menu {
// 将 label 的文字存储到粘贴板
[UIPasteboard generalPasteboard].string = self.label.text;
// 清空文字
self.label.text = nil;
}
- (void)copy:(UIMenuController *)menu {
// 将 label 的文字存储到粘贴板
[UIPasteboard generalPasteboard].string = self.label.text;
}
- (void)paste:(UIMenuController *)menu {
// 将粘贴板的文字赋值给 label
self.label.text = [UIPasteboard generalPasteboard].string;
}
- (void)ding:(UIMenuController *)menu {
NSLog(@"%s %@", __func__, menu);
}
- (void)reply:(UIMenuController *)menu {
NSLog(@"%s %@", __func__, menu);
}
- (void)warn:(UIMenuController *)menu {
NSLog(@"%s %@", __func__, menu);
}
UIKIT_EXTERN NSNotificationName const UIMenuControllerWillShowMenuNotification;
UIKIT_EXTERN NSNotificationName const UIMenuControllerDidShowMenuNotification;
UIKIT_EXTERN NSNotificationName const UIMenuControllerWillHideMenuNotification;
UIKIT_EXTERN NSNotificationName const UIMenuControllerDidHideMenuNotification;
UIKIT_EXTERN NSNotificationName const UIMenuControllerMenuFrameDidChangeNotification;
标签:添加 情况 with ini selector dshow bubuko fir hide
原文地址:https://www.cnblogs.com/CH520/p/9413509.html