首页 > 代码库 > uilabel 复制

uilabel 复制

//添加一个长按响应方法

- (void)addLongPressGestureRecognizer

{

    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc ]initWithTarget:self action:@selector(longPress:)];

    [self.contentLabel addGestureRecognizer:longPress];

    self.contentLabel.userInteractionEnabled = YES;

}

//长按方法的实现

- (void)longPress:(UILongPressGestureRecognizer *)sender

{

    if (sender.state == UIGestureRecognizerStateBegan)

    {

        [self becomeFirstResponder];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuItemsHiden) name:UIMenuControllerWillHideMenuNotification object:nil];

        UIMenuItem *copy = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(menuCopy:)];

        UIMenuItem * report = [[UIMenuItem alloc]initWithTitle:@"举报" action:@selector(menuReport:)];

        UIMenuController *menu = [UIMenuController sharedMenuController];

        [menu setMenuItems:@[copy,report]];

        [menu setTargetRect:CGRectMake([sender locationInView:self.view].x, [sender locationInView:self.view].y, 0, 0) inView:self.view];

        [menu setMenuVisible:YES animated:YES];

        self.contentLabel.backgroundColor = [UIColor grayColor];

        

    }

}

/*!

 *  允许成为第一响应

 */

- (BOOL)canBecomeFirstResponder{

    return YES;

}

/*!

 *  用于控制哪些命令显示在编辑菜单中

 */

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{

    if (action == @selector(menuCopy:)) {

        return YES;

    }

    if (action == @selector(menuReport:)) {

        return YES;

    }

    return NO;

}

/**

 *  @brief 复制的响应方法

 *

 *  @param sender

 */

-(void)menuCopy:(id)sender

{

    [UIPasteboard generalPasteboard].string = self.contentLabel.text?self.contentLabel.text:@"";

}

/**

 *  @brief 举报的响应方法

 *

 *  @param sender

 */

-(void)menuReport:(id)sender

{

    //跳转到举报用户添加 举报类型字段

    NSLog(@"举报的响应方法");

}

 

- (void)menuItemsHiden

{

    self.contentLabel.backgroundColor = [UIColor whiteColor];

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

//来源:http://blog.cocoachina.com/article/39466

 

uilabel 复制