uilabel 复制

Posted 新年新气象934060369

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 复制的主要内容,如果未能解决你的问题,请参考以下文章

如何为 UILabel 设置黑色透明? [复制]

IOS 为UILabel添加长按复制功能

如何将 UITextField 数据复制到 UILabel

如何将 UILabel 文本颜色与其背景匹配? [复制]

[转]iOS为UILabel添加长按复制功能

uilabel 复制