Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding contiguous deletion from the middle of a list and scrolling to highlighted token #67

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f868b25
highlight and scroll to deleted tokens
kwgithubusername May 13, 2015
73c603a
consistent deletion of consecutive items
kwgithubusername May 14, 2015
98c0e9a
remove observer in dealloc
kwgithubusername May 14, 2015
204f1d2
add define strings for shrinking notification
kwgithubusername May 14, 2015
b417256
maintain cursor when deleting from end
kwgithubusername May 14, 2015
442d47c
document changes in more detail
kwgithubusername May 14, 2015
fcd84f9
use this commit if you only want contiguous deletion feature
kwgithubusername May 14, 2015
646b5c7
use this commit for both contiguous deletion and scrolling to highlig…
kwgithubusername May 14, 2015
ea05c1a
remove unnecessary notification
kwgithubusername May 14, 2015
4eccea3
maintain normal deletion behavior at the end of the list
kwgithubusername May 14, 2015
2252927
use this newer commit for contiguous deletion in middle of list only
kwgithubusername May 14, 2015
488ba66
use this newer commit for contiguous deletion in middle of list and s…
kwgithubusername May 14, 2015
f150cc5
separate features into methods
kwgithubusername May 14, 2015
d0e5c99
fix if conditions that were preventing the prior token index from bei…
kwgithubusername May 14, 2015
2cce859
fix missing highlighted token when second to last token selected
kwgithubusername May 14, 2015
1ac36d3
need to anticipate middle deletions for longer lists, will attempt to…
kwgithubusername May 14, 2015
c1fd9e4
prevent indexoftokenpriortodeletedtoken from being set to less than zero
kwgithubusername May 14, 2015
a3c754e
prevent continued highlighting from occuring when adding tokens
kwgithubusername May 14, 2015
e05e10d
use contentSize instead of bounds for detecting height changes to cov…
kwgithubusername May 14, 2015
f337b71
focusInputTextField only when adding items
kwgithubusername May 14, 2015
4eca987
focus on textfield when typing
kwgithubusername May 14, 2015
0220f16
revert changes to original
kwgithubusername May 15, 2015
c0d4cac
revert pbxproj to original
kwgithubusername May 15, 2015
c5c068d
remove dated comment
kwgithubusername May 15, 2015
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
2 changes: 2 additions & 0 deletions VENTokenField/VENTokenField.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
@property (copy, nonatomic) NSString *placeholderText;
@property (copy, nonatomic) NSString *inputTextFieldAccessibilityLabel;

@property (nonatomic) BOOL isDeletingTokens;

- (void)setColorScheme:(UIColor *)color;

@end
Expand Down
45 changes: 41 additions & 4 deletions VENTokenField/VENTokenField.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ @interface VENTokenField () <VENBackspaceTextFieldDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) NSMutableArray *tokens;
@property (assign, nonatomic) CGFloat originalHeight;
@property (nonatomic) CGFloat oldHeight;
@property (strong, nonatomic) UITapGestureRecognizer *tapGestureRecognizer;
@property (strong, nonatomic) VENBackspaceTextField *invisibleTextField;
@property (strong, nonatomic) VENBackspaceTextField *inputTextField;
@property (strong, nonatomic) UIColor *colorScheme;
@property (strong, nonatomic) UILabel *collapsedLabel;

@property (nonatomic) NSInteger indexOfTokenPriorToDeletedToken;
@end


Expand Down Expand Up @@ -100,12 +101,14 @@ - (void)setUpInit
_toLabelText = NSLocalizedString(@"To:", nil);

self.originalHeight = CGRectGetHeight(self.frame);
self.oldHeight = self.originalHeight;

// Add invisible text field to handle backspace when we don't have a real first responder.
[self layoutInvisibleTextField];

[self layoutScrollView];
[self reloadData];

}

- (void)collapse
Expand Down Expand Up @@ -173,13 +176,25 @@ - (NSString *)inputText

- (void)layoutSubviews
{
CGFloat newHeight = self.scrollView.contentSize.height;
[super layoutSubviews];
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.frame) - self.horizontalInset * 2, CGRectGetHeight(self.frame) - self.verticalInset * 2);
if ([self isCollapsed]) {
[self layoutCollapsedLabel];
} else {
[self layoutTokensAndInputWithFrameAdjustment:NO];
}
if (newHeight < self.oldHeight && self.isDeletingTokens )
{
[self continueHighlightingTokensDuringDeletionInMiddleOfListWhenHeightIsReduced];
}
self.oldHeight = self.scrollView.contentSize.height;
}

- (void)continueHighlightingTokensDuringDeletionInMiddleOfListWhenHeightIsReduced
{
VENToken *tokenToHighlight = self.tokens[self.indexOfTokenPriorToDeletedToken];
tokenToHighlight.highlighted = YES;
}

- (void)layoutCollapsedLabel
Expand Down Expand Up @@ -381,8 +396,10 @@ - (void)adjustHeightForCurrentY:(CGFloat)currentY
}
} else { // needs to shrink
if (currentY + [self heightForToken] > self.originalHeight) {
// This is called no matter whether the frame height is being reduced or not when deleting tokens
[self setHeight:currentY + [self heightForToken] + self.verticalInset * 2];
} else {
// This is called only when the frame is being reduced to its original height
[self setHeight:self.originalHeight];
}
}
Expand Down Expand Up @@ -457,7 +474,6 @@ - (void)unhighlightAllTokens
for (VENToken *token in self.tokens) {
token.highlighted = NO;
}

[self setCursorVisibility];
}

Expand All @@ -472,9 +488,17 @@ - (void)setCursorVisibility
[self inputTextFieldBecomeFirstResponder];
} else {
[self.invisibleTextField becomeFirstResponder];
[self scrollToHighlightedToken:[highlightedTokens firstObject]];
}
}

- (void)scrollToHighlightedToken:(VENToken *)token
{
VENToken *tokenOfInterest = token;
CGRect locationOfToken = tokenOfInterest.frame;
[self.scrollView scrollRectToVisible:locationOfToken animated:YES];
}

- (void)updateInputTextField
{
self.inputTextField.placeholder = [self.tokens count] ? nil : self.placeholderText;
Expand All @@ -484,7 +508,8 @@ - (void)focusInputTextField
{
CGPoint contentOffset = self.scrollView.contentOffset;
CGFloat targetY = self.inputTextField.y + [self heightForToken] - self.maxHeight;
if (targetY > contentOffset.y) {

if (targetY > contentOffset.y && !self.isDeletingTokens) {
[self.scrollView setContentOffset:CGPointMake(contentOffset.x, targetY) animated:NO];
}
}
Expand Down Expand Up @@ -551,6 +576,8 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self unhighlightAllTokens];
self.isDeletingTokens = NO;
[self focusInputTextField];
return YES;
}

Expand All @@ -563,7 +590,17 @@ - (void)textFieldDidEnterBackspace:(VENBackspaceTextField *)textField
BOOL didDeleteToken = NO;
for (VENToken *token in self.tokens) {
if (token.highlighted) {
[self.delegate tokenField:self didDeleteTokenAtIndex:[self.tokens indexOfObject:token]];
NSInteger indexOfToken = [self.tokens indexOfObject:token];
[self.delegate tokenField:self didDeleteTokenAtIndex:indexOfToken];
// Highlight the token prior to the token deleted, only if we are deleting from the middle of the list

self.indexOfTokenPriorToDeletedToken = indexOfToken > 0 ? indexOfToken-1 : 0;

if (indexOfToken < self.tokens.count) {
VENToken *tokenBeforeDeletedToken = self.tokens[self.indexOfTokenPriorToDeletedToken];
tokenBeforeDeletedToken.highlighted = YES;
}

didDeleteToken = YES;
break;
}
Expand Down
2 changes: 2 additions & 0 deletions VENTokenFieldSample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ - (IBAction)didTapResignFirstResponderButton:(id)sender

- (void)tokenField:(VENTokenField *)tokenField didEnterText:(NSString *)text
{
self.tokenField.isDeletingTokens = NO;
[self.names addObject:text];
[self.tokenField reloadData];
}

- (void)tokenField:(VENTokenField *)tokenField didDeleteTokenAtIndex:(NSUInteger)index
{
self.tokenField.isDeletingTokens = YES;
[self.names removeObjectAtIndex:index];
[self.tokenField reloadData];
}
Expand Down