diff --git a/README.md b/README.md index a27bb94..315e702 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ This protocol allows you to provide info about what you want to present in the t Implement... * ```tokenField:titleForTokenAtIndex:``` to specify what the title for the token at a particular index should be. +* ```tokenField:underlyingStringForTokenAtIndex:``` to specify what will be copied with the long press on the token at a particular index. * ```numberOfTokensInTokenField:``` to specify how many tokens you have. * ```tokenFieldCollapsedText:``` to specify what you want the token field to say in the collapsed state. diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index 21768cb..2f931e5 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -26,8 +26,10 @@ @property (assign, nonatomic) BOOL highlighted; @property (copy, nonatomic) void (^didTapTokenBlock) (void); +@property (copy, nonatomic) void (^didLongPressTokenBlock) (void); @property (strong, nonatomic) UIColor *colorScheme; - (void)setTitleText:(NSString *)text; +- (void)setTokenCopyString:(NSString *)string; @end diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index 4435a83..97a2f88 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -24,8 +24,10 @@ @interface VENToken () @property (strong, nonatomic) UITapGestureRecognizer *tapGestureRecognizer; +@property (strong, nonatomic) UILongPressGestureRecognizer *longPressGestureRecognizer; @property (strong, nonatomic) IBOutlet UILabel *titleLabel; @property (strong, nonatomic) IBOutlet UIView *backgroundView; +@property (strong, nonatomic) NSString *tokenCopyString; @end @implementation VENToken @@ -43,9 +45,11 @@ - (void)setUpInit { self.backgroundView.layer.cornerRadius = 5; self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapToken:)]; + self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPressToken:)]; self.colorScheme = [UIColor blueColor]; self.titleLabel.textColor = self.colorScheme; [self addGestureRecognizer:self.tapGestureRecognizer]; + [self addGestureRecognizer:self.longPressGestureRecognizer]; } - (void)setTitleText:(NSString *)text @@ -57,6 +61,11 @@ - (void)setTitleText:(NSString *)text [self.titleLabel sizeToFit]; } +- (void)setTokenCopyString:(NSString *)string +{ + self.tokenCopyString = string; +} + - (void)setHighlighted:(BOOL)highlighted { _highlighted = highlighted; @@ -73,6 +82,22 @@ - (void)setColorScheme:(UIColor *)colorScheme [self setHighlighted:_highlighted]; } +#pragma mark - UIMenuController Actions + +- (BOOL)canBecomeFirstResponder +{ + return YES; +} + +- (BOOL)canPerformAction:(SEL)action withSender:(id)sender +{ + return (action == @selector(copy:)); +} + +- (void)copy:(id)sender +{ + [[UIPasteboard generalPasteboard] setString:self.tokenCopyString]; +} #pragma mark - Private @@ -83,4 +108,16 @@ - (void)didTapToken:(UITapGestureRecognizer *)tapGestureRecognizer } } +- (void)didLongPressToken:(UILongPressGestureRecognizer *)longPressGestureRecognizer +{ + if (self.didLongPressTokenBlock) { + self.didLongPressTokenBlock(); + return; + } + [self becomeFirstResponder]; + UIMenuController *menuController = [UIMenuController sharedMenuController]; + [menuController setTargetRect:self.frame inView:self.superview]; + [menuController setMenuVisible:YES animated:YES]; +} + @end diff --git a/VENTokenField/VENTokenField.h b/VENTokenField/VENTokenField.h index b4c81c4..c08485b 100644 --- a/VENTokenField/VENTokenField.h +++ b/VENTokenField/VENTokenField.h @@ -34,6 +34,7 @@ @protocol VENTokenFieldDataSource @optional - (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUInteger)index; +- (NSString *)tokenField:(VENTokenField *)tokenField copyStringForTokenAtIndex:(NSUInteger)index; - (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField; - (NSString *)tokenFieldCollapsedText:(VENTokenField *)tokenField; @end diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index 3512062..99938fc 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -297,6 +297,7 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current { for (NSUInteger i = 0; i < [self numberOfTokens]; i++) { NSString *title = [self titleForTokenAtIndex:i]; + NSString *tokenCopyString = [self copyStringForTokenAtIndex:i]; VENToken *token = [[VENToken alloc] init]; token.colorScheme = self.colorScheme; @@ -307,6 +308,7 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current }; [token setTitleText:[NSString stringWithFormat:@"%@,", title]]; + [token setTokenCopyString:[NSString stringWithFormat:@"%@,", tokenCopyString]]; [self.tokens addObject:token]; if (*currentX + token.width <= self.scrollView.contentSize.width) { // token fits in current line @@ -496,6 +498,16 @@ - (NSString *)titleForTokenAtIndex:(NSUInteger)index return [NSString string]; } +- (NSString *)copyStringForTokenAtIndex:(NSUInteger)index +{ + if ([self.dataSource respondsToSelector:@selector(tokenField:copyStringForTokenAtIndex:)]) { + return [self.dataSource tokenField:self copyStringForTokenAtIndex:index]; + } else if ([self.dataSource respondsToSelector:@selector(tokenField:titleForTokenAtIndex:)]) { + return [self.dataSource tokenField:self titleForTokenAtIndex:index]; + } + return [NSString string]; +} + - (NSUInteger)numberOfTokens { if ([self.dataSource respondsToSelector:@selector(numberOfTokensInTokenField:)]) { diff --git a/VENTokenFieldSample/ViewController.m b/VENTokenFieldSample/ViewController.m index 073f85f..f3e30e3 100644 --- a/VENTokenFieldSample/ViewController.m +++ b/VENTokenFieldSample/ViewController.m @@ -61,6 +61,11 @@ - (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUIn return self.names[index]; } +- (NSString *)tokenField:(VENTokenField *)tokenField copyStringForTokenAtIndex:(NSUInteger)index +{ + return [NSString stringWithFormat:@"The name is: %@", self.names[index]]; +} + - (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField { return [self.names count];