diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index afcc026..a33be90 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -282,15 +282,15 @@ - (void)layoutToLabelInView:(UIView *)view origin:(CGPoint)origin currentX:(CGFl { [self.toLabel removeFromSuperview]; self.toLabel = [self toLabel]; - + CGRect newFrame = self.toLabel.frame; newFrame.origin = origin; - + [self.toLabel sizeToFit]; newFrame.size.width = CGRectGetWidth(self.toLabel.frame); - + self.toLabel.frame = newFrame; - + [view addSubview:self.toLabel]; *currentX += self.toLabel.hidden ? CGRectGetMinX(self.toLabel.frame) : CGRectGetMaxX(self.toLabel.frame) + VENTokenFieldDefaultToLabelPadding; } @@ -309,7 +309,7 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current [token setTitleText:[NSString stringWithFormat:@"%@,", title]]; token.colorScheme = [self colorSchemeForTokenAtIndex:i]; - + [self.tokens addObject:token]; if (*currentX + token.width <= self.scrollView.contentSize.width) { // token fits in current line @@ -341,6 +341,7 @@ - (void)layoutInvisibleTextField self.invisibleTextField = [[VENBackspaceTextField alloc] initWithFrame:CGRectZero]; [self.invisibleTextField setAutocorrectionType:self.autocorrectionType]; [self.invisibleTextField setAutocapitalizationType:self.autocapitalizationType]; + self.invisibleTextField.delegate = self; self.invisibleTextField.backspaceDelegate = self; [self addSubview:self.invisibleTextField]; } @@ -473,7 +474,7 @@ - (void)unhighlightAllTokens for (VENToken *token in self.tokens) { token.highlighted = NO; } - + [self setCursorVisibility]; } @@ -482,7 +483,7 @@ - (void)setCursorVisibility NSArray *highlightedTokens = [self.tokens filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(VENToken *evaluatedObject, NSDictionary *bindings) { return evaluatedObject.highlighted; }]]; - + BOOL visible = [highlightedTokens count] == 0; if (visible) { [self inputTextFieldBecomeFirstResponder]; @@ -510,7 +511,7 @@ - (UIColor *)colorSchemeForTokenAtIndex:(NSUInteger)index { if ([self.dataSource respondsToSelector:@selector(tokenField:colorSchemeForTokenAtIndex:)]) { return [self.dataSource tokenField:self colorSchemeForTokenAtIndex:index]; } - + return self.colorScheme; } @@ -521,7 +522,7 @@ - (NSString *)titleForTokenAtIndex:(NSUInteger)index if ([self.dataSource respondsToSelector:@selector(tokenField:titleForTokenAtIndex:)]) { return [self.dataSource tokenField:self titleForTokenAtIndex:index]; } - + return [NSString string]; } @@ -530,7 +531,7 @@ - (NSUInteger)numberOfTokens if ([self.dataSource respondsToSelector:@selector(numberOfTokensInTokenField:)]) { return [self.dataSource numberOfTokensInTokenField:self]; } - + return 0; } @@ -539,7 +540,7 @@ - (NSString *)collapsedText if ([self.dataSource respondsToSelector:@selector(tokenFieldCollapsedText:)]) { return [self.dataSource tokenFieldCollapsedText:self]; } - + return @""; } @@ -553,7 +554,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField [self.delegate tokenField:self didEnterText:textField.text]; } } - + return NO; } @@ -566,6 +567,12 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { + if ([textField isKindOfClass:[VENBackspaceTextField class]] && + [self isBackspacePressInTextField:textField range:range replacementString:string]) { + [self textFieldDidEnterBackspace:(VENBackspaceTextField *)textField]; + return NO; + } + [self unhighlightAllTokens]; NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; for (NSString *delimiter in self.delimiters) { @@ -583,6 +590,13 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang return YES; } +- (BOOL)isBackspacePressInTextField:(UITextField *)textField range:(NSRange)range replacementString:(NSString *)string { + return textField.text.length == 0 && + range.location == 0 && + range.length == 0 && + string.length == 0; +} + #pragma mark - VENBackspaceTextFieldDelegate