UIMenuController的使用

来源:互联网 发布:net snmp windows 64 编辑:程序博客网 时间:2024/05/21 22:46

很多情况我们都用到了如图所示的选择框

  • 这个菜单就是UIMenuController,系统默认支持UITextField、UITextView、UIWebView控件的UIMenuController相关操作
  • 对于系统不支持UIMenuController操作的控件,我们就要自定义控件的UIMenuController来实现相关功能
这里我用imageview做:代码如下


#import "CajImageView.h"

@interface CajImageView ()

@property (nonatomic,strong) UILongPressGestureRecognizer *longG;

@property (nonatomic,strong) UIMenuController *menuC;

@end

@implementation CajImageView


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        [self setupView];

    }

    return self;

}


- (void)setupView

{

    self.userInteractionEnabled =YES;

    [self addGestureRecognizer:self.longG];

}

//不是所有的控件都会成为第一响应者,需要在此声明

- (BOOL)canBecomeFirstResponder

{

    return YES;

}


- (void)longGrespongse

{

    [self becomeFirstResponder];

    _menuC = [UIMenuControllersharedMenuController];

    UIMenuItem *lineItem = [[UIMenuItemalloc] initWithTitle:@"下划线"action:@selector(drawLine)];

    UIMenuItem *highLightItem = [[UIMenuItemalloc] initWithTitle:@"高亮"action:@selector(highLight)];

    UIMenuItem *rectItem = [[UIMenuItemalloc] initWithTitle:@"矩形框"action:@selector(drawRectLine)];

    [_menuC setMenuItems:@[lineItem, highLightItem, rectItem]];

    [_menuC setTargetRect:CGRectMake(0,self.frame.size.height*0.5,self.frame.size.width,self.frame.size.height)inView:self];

    [_menuC setMenuVisible:YESanimated:YES];

}

//这个方法必须有,不然不会显示菜单

- (void)drawLine

{

    

}


- (void)highLight

{

    

}


- (void)drawRectLine

{

    

}

#pragma mark - laze

- (UILongPressGestureRecognizer *)longG

{

    if(!_longG)

        _longG = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longGrespongse)];

    return _longG;

}