From fc57eeef38bf1f4d27018a03bfc7d5498dca90ed Mon Sep 17 00:00:00 2001 From: Egor Tolstoy Date: Sat, 28 Mar 2015 22:57:54 +0300 Subject: [PATCH 1/6] Added VENTokenFieldDataSource method for obtaining "underlying" string for token - it will be used to get the real value for copying --- VENTokenField/VENTokenField.h | 1 + VENTokenField/VENTokenField.m | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/VENTokenField/VENTokenField.h b/VENTokenField/VENTokenField.h index b4c81c4..4b15262 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 underlyingStringForTokenAtIndex:(NSUInteger)index; - (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField; - (NSString *)tokenFieldCollapsedText:(VENTokenField *)tokenField; @end diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index 3512062..5523a89 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -496,6 +496,14 @@ - (NSString *)titleForTokenAtIndex:(NSUInteger)index return [NSString string]; } +- (NSString *)underlyingStringForTokenAtIndex:(NSUInteger)index +{ + if ([self.dataSource respondsToSelector:@selector(tokenField:underlyingStringForTokenAtIndex:)]) { + return [self.dataSource tokenField:self underlyingStringForTokenAtIndex:index]; + } + return [NSString string]; +} + - (NSUInteger)numberOfTokens { if ([self.dataSource respondsToSelector:@selector(numberOfTokensInTokenField:)]) { From 5e95e2d4c00e262387fe3395b22f7a43aedad831 Mon Sep 17 00:00:00 2001 From: Egor Tolstoy Date: Sat, 28 Mar 2015 23:00:40 +0300 Subject: [PATCH 2/6] Added the underlyingText property for VENToken --- VENTokenField/VENToken.h | 1 + VENTokenField/VENToken.m | 6 ++++++ VENTokenField/VENTokenField.m | 2 ++ 3 files changed, 9 insertions(+) diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index 21768cb..1bf3840 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -29,5 +29,6 @@ @property (strong, nonatomic) UIColor *colorScheme; - (void)setTitleText:(NSString *)text; +- (void)setUnderlyingText:(NSString *)text; @end diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index 4435a83..75c0414 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -26,6 +26,7 @@ @interface VENToken () @property (strong, nonatomic) UITapGestureRecognizer *tapGestureRecognizer; @property (strong, nonatomic) IBOutlet UILabel *titleLabel; @property (strong, nonatomic) IBOutlet UIView *backgroundView; +@property (strong, nonatomic) NSString *underlyingText; @end @implementation VENToken @@ -57,6 +58,11 @@ - (void)setTitleText:(NSString *)text [self.titleLabel sizeToFit]; } +- (void)setUnderlyingText:(NSString *)text +{ + self.underlyingText = text; +} + - (void)setHighlighted:(BOOL)highlighted { _highlighted = highlighted; diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index 5523a89..780a206 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 *underlyingString = [self underlyingStringForTokenAtIndex: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 setUnderlyingText:[NSString stringWithFormat:@"%@,", underlyingString]]; [self.tokens addObject:token]; if (*currentX + token.width <= self.scrollView.contentSize.width) { // token fits in current line From e36568491d45aaa88095476721706eed3ad40e0a Mon Sep 17 00:00:00 2001 From: Egor Tolstoy Date: Sat, 28 Mar 2015 23:17:00 +0300 Subject: [PATCH 3/6] Implemented copy functional for VENToken on long press --- VENTokenField/VENToken.h | 3 ++- VENTokenField/VENToken.m | 35 ++++++++++++++++++++++++++-- VENTokenField/VENTokenField.m | 2 +- VENTokenFieldSample/ViewController.m | 5 ++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index 1bf3840..aef6cd3 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -26,9 +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)setUnderlyingText:(NSString *)text; +- (void)setUnderlyingString:(NSString *)string; @end diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index 75c0414..f04dfb5 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -24,6 +24,7 @@ @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 *underlyingText; @@ -44,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 @@ -58,9 +61,9 @@ - (void)setTitleText:(NSString *)text [self.titleLabel sizeToFit]; } -- (void)setUnderlyingText:(NSString *)text +- (void)setUnderlyingString:(NSString *)string { - self.underlyingText = text; + self.underlyingText = string; } - (void)setHighlighted:(BOOL)highlighted @@ -79,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.underlyingText]; +} #pragma mark - Private @@ -89,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.m b/VENTokenField/VENTokenField.m index 780a206..1641b3b 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -308,7 +308,7 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current }; [token setTitleText:[NSString stringWithFormat:@"%@,", title]]; - [token setUnderlyingText:[NSString stringWithFormat:@"%@,", underlyingString]]; + [token setUnderlyingString:[NSString stringWithFormat:@"%@,", underlyingString]]; [self.tokens addObject:token]; if (*currentX + token.width <= self.scrollView.contentSize.width) { // token fits in current line diff --git a/VENTokenFieldSample/ViewController.m b/VENTokenFieldSample/ViewController.m index 073f85f..50b0670 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 underlyingStringForTokenAtIndex:(NSUInteger)index +{ + return [NSString stringWithFormat:@"The name is: %@", self.names[index]]; +} + - (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField { return [self.names count]; From 875692a0c02eaf8c309600fae8031150d8858873 Mon Sep 17 00:00:00 2001 From: Egor Tolstoy Date: Sat, 28 Mar 2015 23:21:27 +0300 Subject: [PATCH 4/6] Updated README.md with the new datasource method --- README.md | 1 + 1 file changed, 1 insertion(+) 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. From c0f749dc0502693f75b73f7f11ed345cc26fb163 Mon Sep 17 00:00:00 2001 From: etolstoy Date: Thu, 16 Apr 2015 10:06:52 +0300 Subject: [PATCH 5/6] Implemented using token title as the default for copy string --- VENTokenField/VENTokenField.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index 1641b3b..66c4a90 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -502,6 +502,8 @@ - (NSString *)underlyingStringForTokenAtIndex:(NSUInteger)index { if ([self.dataSource respondsToSelector:@selector(tokenField:underlyingStringForTokenAtIndex:)]) { return [self.dataSource tokenField:self underlyingStringForTokenAtIndex:index]; + } else if ([self.dataSource respondsToSelector:@selector(tokenField:titleForTokenAtIndex:)]) { + return [self.dataSource tokenField:self titleForTokenAtIndex:index]; } return [NSString string]; } From 9c32feac0d5c8023919404912d7ab3de3104eda5 Mon Sep 17 00:00:00 2001 From: etolstoy Date: Thu, 16 Apr 2015 10:11:39 +0300 Subject: [PATCH 6/6] Changed the name of the delegate method from 'underlyingString' to 'copyString' --- VENTokenField/VENToken.h | 2 +- VENTokenField/VENToken.m | 8 ++++---- VENTokenField/VENTokenField.h | 2 +- VENTokenField/VENTokenField.m | 10 +++++----- VENTokenFieldSample/ViewController.m | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index aef6cd3..2f931e5 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -30,6 +30,6 @@ @property (strong, nonatomic) UIColor *colorScheme; - (void)setTitleText:(NSString *)text; -- (void)setUnderlyingString:(NSString *)string; +- (void)setTokenCopyString:(NSString *)string; @end diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index f04dfb5..97a2f88 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -27,7 +27,7 @@ @interface VENToken () @property (strong, nonatomic) UILongPressGestureRecognizer *longPressGestureRecognizer; @property (strong, nonatomic) IBOutlet UILabel *titleLabel; @property (strong, nonatomic) IBOutlet UIView *backgroundView; -@property (strong, nonatomic) NSString *underlyingText; +@property (strong, nonatomic) NSString *tokenCopyString; @end @implementation VENToken @@ -61,9 +61,9 @@ - (void)setTitleText:(NSString *)text [self.titleLabel sizeToFit]; } -- (void)setUnderlyingString:(NSString *)string +- (void)setTokenCopyString:(NSString *)string { - self.underlyingText = string; + self.tokenCopyString = string; } - (void)setHighlighted:(BOOL)highlighted @@ -96,7 +96,7 @@ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender - (void)copy:(id)sender { - [[UIPasteboard generalPasteboard] setString:self.underlyingText]; + [[UIPasteboard generalPasteboard] setString:self.tokenCopyString]; } #pragma mark - Private diff --git a/VENTokenField/VENTokenField.h b/VENTokenField/VENTokenField.h index 4b15262..c08485b 100644 --- a/VENTokenField/VENTokenField.h +++ b/VENTokenField/VENTokenField.h @@ -34,7 +34,7 @@ @protocol VENTokenFieldDataSource @optional - (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUInteger)index; -- (NSString *)tokenField:(VENTokenField *)tokenField underlyingStringForTokenAtIndex:(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 66c4a90..99938fc 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -297,7 +297,7 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current { for (NSUInteger i = 0; i < [self numberOfTokens]; i++) { NSString *title = [self titleForTokenAtIndex:i]; - NSString *underlyingString = [self underlyingStringForTokenAtIndex:i]; + NSString *tokenCopyString = [self copyStringForTokenAtIndex:i]; VENToken *token = [[VENToken alloc] init]; token.colorScheme = self.colorScheme; @@ -308,7 +308,7 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current }; [token setTitleText:[NSString stringWithFormat:@"%@,", title]]; - [token setUnderlyingString:[NSString stringWithFormat:@"%@,", underlyingString]]; + [token setTokenCopyString:[NSString stringWithFormat:@"%@,", tokenCopyString]]; [self.tokens addObject:token]; if (*currentX + token.width <= self.scrollView.contentSize.width) { // token fits in current line @@ -498,10 +498,10 @@ - (NSString *)titleForTokenAtIndex:(NSUInteger)index return [NSString string]; } -- (NSString *)underlyingStringForTokenAtIndex:(NSUInteger)index +- (NSString *)copyStringForTokenAtIndex:(NSUInteger)index { - if ([self.dataSource respondsToSelector:@selector(tokenField:underlyingStringForTokenAtIndex:)]) { - return [self.dataSource tokenField:self underlyingStringForTokenAtIndex: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]; } diff --git a/VENTokenFieldSample/ViewController.m b/VENTokenFieldSample/ViewController.m index 50b0670..f3e30e3 100644 --- a/VENTokenFieldSample/ViewController.m +++ b/VENTokenFieldSample/ViewController.m @@ -61,7 +61,7 @@ - (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUIn return self.names[index]; } -- (NSString *)tokenField:(VENTokenField *)tokenField underlyingStringForTokenAtIndex:(NSUInteger)index +- (NSString *)tokenField:(VENTokenField *)tokenField copyStringForTokenAtIndex:(NSUInteger)index { return [NSString stringWithFormat:@"The name is: %@", self.names[index]]; }