IOS MenuController初步了解

IOS MenuController初步了解


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

  • 让其他控件也支持MenuController

      例如:UILabel
    
  • 自定义label

  • 重写两个方法
 //允许自己成为第一响应者
- (BOOL)canBecomeFirstResponder{
    return YES;
}
//Label能够执行哪些操作(menu)
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{

    if (action == @selector(copy:) || action == @selector(cut:)) {
        return YES;
    }

    return NO;
}
  • 实现各种操作方法
 - (void)copy:(id)sender{
    //复制版
    UIPasteboard * paste = [UIPasteboard generalPasteboard];

    paste.string = self.text;

}

- (void)cut:(id)sender{

    UIPasteboard * paste = [UIPasteboard generalPasteboard];

    paste.string = self.text;

    self.text = nil;

}

- (void)paste:(id)sender{

    UIPasteboard * paste = [UIPasteboard generalPasteboard];

    self.text = paste.string;

}
  • 让Label成为第一响应者
 //让自己成为第一响应者
    [self becomeFirstResponder];
  • 显示MenuController
     //初始化menu
    UIMenuController * menu = [UIMenuController sharedMenuController];
    //设置menu的显示位置
    [menu setTargetRect:self.frame inView:self.superview];
    //让menu显示并且伴有动画
    [menu setMenuVisible:YES animated:YES];
posted @ 2016-05-03 17:09  旭宝爱吃鱼  阅读(740)  评论(0编辑  收藏  举报