iOS Label 긴 복제, 붙여넣기, 잘라내기

2593 단어
코드는 다음과 같습니다(확장된.m 파일의 구현 부분).
@implementation CustomEditLabel

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib{
    [super awakeFromNib];
    
    [self addLongPressEvent];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        [self addLongPressEvent];
    }
    return self;
}

- (void)addLongPressEvent{
    
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(actionS:)];
    [self addGestureRecognizer:longPress];
    
}


- (void)actionS:(UILongPressGestureRecognizer *)gesture{
    
    if (gesture.state == UIGestureRecognizerStateBegan) {
    
    [self becomeFirstResponder];
    UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"   " action:@selector(copyS:)];
    UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"   " action:@selector(pasteS:)];
    UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"   " action:@selector(cutS:)];
    UIMenuController *menuC = [UIMenuController sharedMenuController];

    menuC.menuItems = @[menuItem1, menuItem2, menuItem3];
    menuC.arrowDirection = UIMenuControllerArrowUp;
    
    if (menuC.menuVisible) {
//        NSLog(@"menuC.menuVisible       --  %d", menuC.menuVisible);
        return ;
    }
    
    [menuC setTargetRect:self.frame inView:self.superview];
    [menuC setMenuVisible:YES animated:YES];
        
   }
}

- (BOOL)canBecomeFirstResponder{
    return YES;
    
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    
    if ( action == @selector(copyS:)  || (action == @selector(pasteS:) && [UIPasteboard generalPasteboard].string) || action == @selector(cutS:) ) {
//        NSLog(@"      --  %@", [UIPasteboard generalPasteboard].string);
        return YES;
    }else{
        return NO;
    }
}
//    (    )
- (void)cutS:(id)sender{
    
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = self.text;
    //     
    self.text = nil;
}

//  
- (void)copyS:(id)sender{
    
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = self.text;
    
}
//  
- (void)pasteS:(id)sender{
    
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    self.text = pboard.string;
}


@end

불완전한 점이 있으면 잘 부탁드립니다...

좋은 웹페이지 즐겨찾기