ios 给label添加复制动作

BYXCMenuItem.h

@interface BYXCMenuItem : UIMenuItem

//@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) id attachObj;

@end

BYXCMenuItem.m

#import "BYXCMenuItem.h"

@implementation BYXCMenuItem

@end

FNAgendaDetailTitleTableViewCell.h

#import "FNAgendaDetailTitleTableViewCell.h"
#import <Masonry.h>
#import "BYXCMenuItem.h"

@interface FNAgendaDetailTitleTableViewCell ()

@property (nonatomic, assign) CGFloat cellheight;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation FNAgendaDetailTitleTableViewCell
//初始化
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.contentView.backgroundColor = [UIColor whiteColor]; self.selectionStyle = UITableViewCellSelectionStyleNone; _titleLabel = [[UILabel alloc] init]; [self.contentView addSubview:_titleLabel]; _titleLabel.tintColor = UIColorFromRGB(0x111111); _titleLabel.font = [UIFont systemFontOfSize:16]; _titleLabel.numberOfLines = 0; _titleLabel.userInteractionEnabled = YES; [self addLongPressRecognizer]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView.mas_left).offset(13); make.centerY.equalTo(self.contentView.mas_centerY); }]; } return self; } //设置数据刷新布局 - (void)setAgendTitle:(NSString *)agendTitle { _agendTitle = agendTitle; _titleLabel.text = agendTitle; CGSize size = [_titleLabel sizeThatFits:CGSizeMake(UIScreenWidth-26, CGFLOAT_MAX)]; _titleLabel.frame = CGRectMake(13, 17, UIScreenWidth-26, size.height); } //返回cell高度 + (CGFloat)cellHeightWithMsg:(NSString *)msg { UILabel *label = [[UILabel alloc] init]; label.text = msg; label.font = [UIFont systemFontOfSize:16]; label.numberOfLines = 0; CGSize size = [label sizeThatFits:CGSizeMake(UIScreenWidth-26, CGFLOAT_MAX)]; return size.height+34; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
//重要
- (BOOL)canBecomeFirstResponder { return YES; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { return (action == @selector(copyText:)); } - (void)addLongPressRecognizer { // 文字内容添加长按事件 UILongPressGestureRecognizer *contentLabelLongPressedRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; [_titleLabel addGestureRecognizer:contentLabelLongPressedRecognizer]; } - (void)handleLongPressGesture:(UIGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateBegan) { [self becomeFirstResponder]; // 颜色加深,表示选中 [_titleLabel setBackgroundColor:[FNColorUtil globalSeparatorLineColor]]; UIMenuController *menuController = [UIMenuController sharedMenuController]; menuController.menuItems = [self textMenuItems]; [menuController setTargetRect:recognizer.view.frame inView:recognizer.view.superview]; [menuController setMenuVisible:YES animated:YES]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideMenuNotification:) name:UIMenuControllerWillHideMenuNotification object:nil]; } } - (void)hideMenuNotification:(id)sender { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerWillHideMenuNotification object:nil]; [_titleLabel setBackgroundColor:[UIColor clearColor]]; } - (NSArray *)textMenuItems { NSMutableArray *items = [NSMutableArray array]; NSString *text = _titleLabel.text; [self addMenuItemWithTitle:@"复制" action:@selector(copyText:) attachObj:text toItems:items]; return items; } - (void)copyText:(UIMenuController *)sender { NSArray *menuItems = sender.menuItems; for (NSInteger i = 0; i < menuItems.count; i++) { BYXCMenuItem *item = menuItems[i]; if ([item.attachObj isKindOfClass:[NSString class]]) { if (item.action == @selector(copyText:)) { [[UIPasteboard generalPasteboard] setString:item.attachObj]; break; } } } } - (void)addMenuItemWithTitle:(NSString *)title action:(SEL)action attachObj:(id)attachObj toItems:(NSMutableArray *)items { BYXCMenuItem *item = [[BYXCMenuItem alloc] initWithTitle:NSLocalizedString(title, nil) action:action]; item.attachObj = attachObj; [items addObject:item]; }@end

 

posted on 2017-12-23 21:37  麦芽呀~  阅读(897)  评论(0编辑  收藏  举报