diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index afcc026..f962a68 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,7 +341,7 @@ - (void)layoutInvisibleTextField self.invisibleTextField = [[VENBackspaceTextField alloc] initWithFrame:CGRectZero]; [self.invisibleTextField setAutocorrectionType:self.autocorrectionType]; [self.invisibleTextField setAutocapitalizationType:self.autocapitalizationType]; - self.invisibleTextField.backspaceDelegate = self; + self.invisibleTextField.delegate = self; [self addSubview:self.invisibleTextField]; } @@ -409,7 +409,6 @@ - (VENBackspaceTextField *)inputTextField _inputTextField.autocapitalizationType = self.autocapitalizationType; _inputTextField.tintColor = self.colorScheme; _inputTextField.delegate = self; - _inputTextField.backspaceDelegate = self; _inputTextField.placeholder = self.placeholderText; _inputTextField.accessibilityLabel = self.inputTextFieldAccessibilityLabel ?: NSLocalizedString(@"To", nil); _inputTextField.inputAccessoryView = self.inputTextFieldAccessoryView; @@ -473,7 +472,7 @@ - (void)unhighlightAllTokens for (VENToken *token in self.tokens) { token.highlighted = NO; } - + [self setCursorVisibility]; } @@ -482,7 +481,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 +509,7 @@ - (UIColor *)colorSchemeForTokenAtIndex:(NSUInteger)index { if ([self.dataSource respondsToSelector:@selector(tokenField:colorSchemeForTokenAtIndex:)]) { return [self.dataSource tokenField:self colorSchemeForTokenAtIndex:index]; } - + return self.colorScheme; } @@ -521,7 +520,7 @@ - (NSString *)titleForTokenAtIndex:(NSUInteger)index if ([self.dataSource respondsToSelector:@selector(tokenField:titleForTokenAtIndex:)]) { return [self.dataSource tokenField:self titleForTokenAtIndex:index]; } - + return [NSString string]; } @@ -530,7 +529,7 @@ - (NSUInteger)numberOfTokens if ([self.dataSource respondsToSelector:@selector(numberOfTokensInTokenField:)]) { return [self.dataSource numberOfTokensInTokenField:self]; } - + return 0; } @@ -539,7 +538,7 @@ - (NSString *)collapsedText if ([self.dataSource respondsToSelector:@selector(tokenFieldCollapsedText:)]) { return [self.dataSource tokenFieldCollapsedText:self]; } - + return @""; } @@ -553,7 +552,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField [self.delegate tokenField:self didEnterText:textField.text]; } } - + return NO; } @@ -566,6 +565,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 +588,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