• IOS MenuController的部分操作


    这里我们要实现的将是选择按钮的自定义

    综合上一节的随笔,这里给出效果图。

    ViewController.m

    //
    //  ViewController.m
    //  CX-MenuController
    //
    //  Created by ma c on 16/4/7.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "CXLabel.h"
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIWebView *webView;
    @property (weak, nonatomic) IBOutlet CXLabel *label;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self.webView loadHTMLString:@"<div>旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼旭宝爱吃鱼</.div>" baseURL:nil];
        
        UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(click)];
        [self.label addGestureRecognizer:longPress];
    
        
    }
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
        [self.view endEditing:YES];
        
    }
    
    - (void)click{
        //让自己成为第一响应者
        [self.label becomeFirstResponder];
        //初始化menu
        UIMenuController * menu = [UIMenuController sharedMenuController];
        
        UIMenuItem * xu = [[UIMenuItem alloc]initWithTitle:@"" action:@selector(xu)];
        UIMenuItem * bao = [[UIMenuItem alloc]initWithTitle:@"" action:@selector(bao)];
        UIMenuItem * ai = [[UIMenuItem alloc]initWithTitle:@"" action:@selector(ai)];
        UIMenuItem * chi = [[UIMenuItem alloc]initWithTitle:@"" action:@selector(chi)];
        UIMenuItem * yu = [[UIMenuItem alloc]initWithTitle:@"" action:@selector(yu)];
        
        menu.menuItems = @[xu,bao,ai,chi,yu];
        
        //设置menu的显示位置
        [menu setTargetRect:self.label.frame inView:self.view];
        //让menu显示并且伴有动画
        [menu setMenuVisible:YES animated:YES];
        
    }
    
    - (void)xu{
        
    }
    - (void)bao{
        
    }
    -(void)ai{
        
    }
    -(void)chi{
        
    }
    -(void)yu{
        
    }
    
    @end

    CXLabel.m

    //
    //  CXLabel.m
    //  CX-MenuController
    //
    //  Created by ma c on 16/4/7.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "CXLabel.h"
    
    @implementation CXLabel
    
    - (void)awakeFromNib{
        [self setup];
    }
    -(instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            [self setup];
        }
        return self;
    }
    
    - (void)setup{
        //允许用户交互
        self.userInteractionEnabled = YES;
    }
    //允许自己成为第一响应者
    - (BOOL)canBecomeFirstResponder{
        return YES;
    }
    //Label能够执行哪些操作(menu)
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
      
    //    if (action == @selector(copy:) || action == @selector(cut:) || action == @selector(paste:)) {
    //        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;
        
    }
    
    
    
    
    @end
  • 相关阅读:
    范围截取 字符串内容
    post请求 application/x‐www‐form‐urlencoded
    未能加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
    获取Url链接后的问号传值中的参数
    Post 提交跳转页面 Jquery请求
    C# POST application/x-www-form-urlencoded 请求
    《exception》第九次团队作业:Beta冲刺与验收准备(大结局)
    《Exception》第八次团队作业:Alpha冲刺
    《Exception团队》第七次作业:团队项目设计完善&编码
    《Exceptioning团队》第六次作业:团队项目系统设计改进与详细设计
  • 原文地址:https://www.cnblogs.com/xubaoaichiyu/p/5364593.html
Copyright © 2020-2023  润新知