Skip to content
This repository has been archived by the owner on Apr 22, 2019. It is now read-only.

Add ability to customize the Copy/Paste menu by subclassing HPGrowingTextView #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions class/HPTextViewInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
#import "HPTextViewInternal.h"


@interface HPTextViewInternal ()
{
BOOL _editActionCallInProgress;
}

@end

@implementation HPTextViewInternal

-(void)setText:(NSString *)text
Expand Down Expand Up @@ -117,4 +124,84 @@ -(void)setPlaceholder:(NSString *)placeholder
[self setNeedsDisplay];
}


#pragma mark - copy/paste delegation support


// The following code allows you to to override the copy/paste functionality by subclassing HPGrowingTextView.
// In your subclass you may override canPerformAction:withSender as well implement any of the UIResponderEditAction
// informal protocol methods. You may safely call the methods in self.internalTextView to get the default implementation.


#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" // yes, we know, but it's ok


-(BOOL)delegatePerformEditAction:(SEL)sel sender:(id)sender
{
if ( !_editActionCallInProgress && [self.delegate respondsToSelector:sel] )
{
_editActionCallInProgress = YES;
[self.delegate performSelector:sel withObject:sender];
_editActionCallInProgress = NO;
return YES; // handled
}
else
return NO; // not handled, call super
}


#pragma clang diagnostic pop


-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if ( !_editActionCallInProgress )
{
_editActionCallInProgress = YES;
BOOL result = [(id)self.delegate canPerformAction:action withSender:sender];
_editActionCallInProgress = NO;

return result;
}
else
return [super canPerformAction:action withSender:sender];
}


-(void)copy:(id)sender
{
if ( ![self delegatePerformEditAction:_cmd sender:sender] )
[super copy:sender];
}


-(void)cut:(id)sender
{
if ( ![self delegatePerformEditAction:_cmd sender:sender] )
[super cut:sender];
}


-(void)paste:(id)sender
{
if ( ![self delegatePerformEditAction:_cmd sender:sender] )
[super paste:sender];
}


-(void)select:(id)sender
{
if ( ![self delegatePerformEditAction:_cmd sender:sender] )
[super select:sender];
}


-(void)selectAll:(id)sender
{
if ( ![self delegatePerformEditAction:_cmd sender:sender] )
[super selectAll:sender];
}


@end