IOS 开发 可复制label

来源:互联网 发布:手机屏幕录制软件 编辑:程序博客网 时间:2024/05/17 21:39

#import "CopyLabel.h"


@implementation CopyLabel


//控件初始化

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [superinitWithFrame:frame]) {

        [selfpressAction];

    }

    returnself;

}


//增加用户交互及长按手势

- (void)pressAction {

    self.userInteractionEnabled =YES;

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressAction:)];

    longPress.minimumPressDuration =1;

    [selfaddGestureRecognizer:longPress];

}


// 使label能够成为响应事件

- (BOOL)canBecomeFirstResponder

{

    returnYES;

}


// 控制响应的方法

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

{

    return action ==@selector(customCopy:);

}


//响应长按手势

- (void)longPressAction:(UIGestureRecognizer *)recognizer {

    [selfbecomeFirstResponder];

    UIMenuItem *copyItem = [[UIMenuItemalloc] initWithTitle:@"拷贝"action:@selector(customCopy:)];

    [[UIMenuControllersharedMenuController] setMenuItems:[NSArrayarrayWithObjects:copyItem,nil]];

    [[UIMenuControllersharedMenuController] setTargetRect:self.frameinView:self.superview];

    [[UIMenuControllersharedMenuController] setMenuVisible:YESanimated:YES];

}


//复制

- (void)customCopy:(id)sender {

    UIPasteboard *pasteboard = [UIPasteboardgeneralPasteboard];

    pasteboard.string =self.text;

}


@end

下载地址:   http://download.csdn.net/detail/qq_20176153/9664627

0 0
原创粉丝点击