UIMenuController的示例

字数 121阅读 62
UIMenuController

UIMenuController须知

  • 默认情况下, 有以下控件已经支持UIMenuController
    • UITextField
    • UITextView
    • UIWebView

让其他控件也支持UIMenuController(比如UILabel)

  • 自定义UILabel
  • 重写2个方法
/**
 * 让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;
}
  • 实现各种操作方法
- (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成为第一响应者
// 这里的self是label
[self becomeFirstResponder];
  • 显示UIMenuController
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
// 添加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];
注意

自定义UIMenuItem,该item的实现方法必须写在控制器中,否则自定义item无法显示

例:

#import "ViewController.h"
#import "ZBLabel.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (weak, nonatomic) IBOutlet ZBLabel *customLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)ding:(UIMenuController *)menu
{
    NSLog(@"%s %@", __func__, menu);
}

- (void)replay:(UIMenuController *)menu
{
    NSLog(@"%s %@", __func__, menu);
}

- (void)report:(UIMenuItem *)menu
{
    NSLog(@"%s %@", __func__ , menu);
}
为了代码的整体结构,这样写更好

#import "ViewController.h"
#import "XMGLabel.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet XMGLabel *label;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
    [self.webView loadHTMLString:@"<div style=\"color:red;\">哈哈哈哈</div>" baseURL:nil];
    
    [self.label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)]];
}

- (void)labelClick
{
    // 1.label要成为第一响应者(作用是:告诉UIMenuController支持哪些操作, 这些操作如何处理)
    [self.label becomeFirstResponder];
    
    // 2.显示MenuController
    UIMenuController *menu = [UIMenuController sharedMenuController];
    
    // 添加MenuItem
    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 setTargetRect:self.label.bounds inView:self.label];
    [menu setMenuVisible:YES animated:YES];
}

- (void)ding:(UIMenuController *)menu
{
    NSLog(@"%s %@", __func__ , menu);
}

- (void)replay:(UIMenuController *)menu
{
    NSLog(@"%s %@", __func__ , menu);
}

- (void)report:(UIMenuController *)menu
{
    NSLog(@"%s %@", __func__ , menu);
}
@end

推荐阅读更多精彩内容