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

Added ability to intercept -[UITextView paste:] #1136

Merged
merged 6 commits into from
Aug 14, 2015
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions JSQMessagesDemo/DemoMessagesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#import "DemoMessagesViewController.h"

@interface DemoMessagesViewController () <JSQMessagesComposerTextViewPasteDelegate>
@end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this to the header file.


@implementation DemoMessagesViewController

Expand All @@ -44,6 +46,7 @@ - (void)viewDidLoad
self.senderId = kJSQDemoAvatarIdSquires;
self.senderDisplayName = kJSQDemoAvatarDisplayNameSquires;

self.inputToolbar.contentView.textView.pasteDelegate = self;

/**
* Load up our fake data for the demo
Expand Down Expand Up @@ -628,4 +631,23 @@ - (void)collectionView:(JSQMessagesCollectionView *)collectionView didTapCellAtI
NSLog(@"Tapped cell at %@!", NSStringFromCGPoint(touchLocation));
}

#pragma mark - JSQMessagesComposerTextViewPasteDelegate methods


- (BOOL)composerTextView:(JSQMessagesComposerTextView *)textView shouldPasteWithSender:(id)sender
{
if ([UIPasteboard generalPasteboard].image) {
// If there's an image in the pasteboard, construct a media item with that image and `send` it.
JSQPhotoMediaItem *item = [[JSQPhotoMediaItem alloc] initWithImage:[UIPasteboard generalPasteboard].image];
JSQMessage *message = [[JSQMessage alloc] initWithSenderId:self.senderId
senderDisplayName:self.senderDisplayName
date:[NSDate date]
media:item];
[self.demoData.messages addObject:message];
[self finishSendingMessage];
return NO;
}
return YES;
}

@end
20 changes: 20 additions & 0 deletions JSQMessagesViewController/Views/JSQMessagesComposerTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@

#import <UIKit/UIKit.h>

@class JSQMessagesComposerTextView;

/**
* A delegate used to forward custom notifications from `JSQMessagesComposerTextView`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A delegate object used to notify the receiver paste events from a JSQMessagesComposerTextView.

*/
@protocol JSQMessagesComposerTextViewPasteDelegate <NSObject>

/**
* Asks the delegate whether or not the `textView` should use the original implementation of -[UITextView paste].
* If you want any custom pasting behavior, implement this delegate method and return `NO`
* when you want to handle pasting yourself.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Wrap "-[UITextView paste]" in ticks

  2. Second sentence should become:

    @discussion Use this delegate method to implement custom pasting behavior. You should return NO when you want to handle pasting. Return YES to defer functionality to the textView.

*/
- (BOOL)composerTextView:(JSQMessagesComposerTextView *)textView shouldPasteWithSender:(id)sender;
@end

/**
* An instance of `JSQMessagesComposerTextView` is a subclass of `UITextView` that is styled and used
* for composing messages in a `JSQMessagesViewController`. It is a subview of a `JSQMessagesToolbarContentView`.
Expand All @@ -34,6 +49,11 @@
*/
@property (strong, nonatomic) UIColor *placeHolderTextColor;

/**
* A delegate that conforms to `JSQMessagesComposerTextViewDelegate`. The default value is `nil`.
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change to:

The object that acts as the paste delegate of the text view.

@property (weak, nonatomic) id<JSQMessagesComposerTextViewPasteDelegate> pasteDelegate;

/**
* Determines whether or not the text view contains text after trimming white space
* from the front and back of its string.
Expand Down
7 changes: 7 additions & 0 deletions JSQMessagesViewController/Views/JSQMessagesComposerTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ - (void)setTextAlignment:(NSTextAlignment)textAlignment
[self setNeedsDisplay];
}

- (void)paste:(id)sender
{
if (!self.pasteDelegate || [self.pasteDelegate composerTextView:self shouldPasteWithSender:sender]) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove !self.pasteDelegate ||. if the delegate is nil, this will evaluate to false.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means that if there's no paste delegate, [super paste:sender] won't get called.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. you're right 👍

[super paste:sender];
}
}

#pragma mark - Drawing

- (void)drawRect:(CGRect)rect
Expand Down