Skip to content

Commit

Permalink
Fixed issue venmo#38 with custom keyboards
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Rentka committed Feb 25, 2016
1 parent 8f9f343 commit bd593bf
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions VENTokenField/VENTokenField.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -473,7 +472,7 @@ - (void)unhighlightAllTokens
for (VENToken *token in self.tokens) {
token.highlighted = NO;
}

[self setCursorVisibility];
}

Expand All @@ -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];
Expand Down Expand Up @@ -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;
}

Expand All @@ -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];
}

Expand All @@ -530,7 +529,7 @@ - (NSUInteger)numberOfTokens
if ([self.dataSource respondsToSelector:@selector(numberOfTokensInTokenField:)]) {
return [self.dataSource numberOfTokensInTokenField:self];
}

return 0;
}

Expand All @@ -539,7 +538,7 @@ - (NSString *)collapsedText
if ([self.dataSource respondsToSelector:@selector(tokenFieldCollapsedText:)]) {
return [self.dataSource tokenFieldCollapsedText:self];
}

return @"";
}

Expand All @@ -553,7 +552,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField
[self.delegate tokenField:self didEnterText:textField.text];
}
}

return NO;
}

Expand All @@ -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) {
Expand All @@ -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

Expand Down

0 comments on commit bd593bf

Please sign in to comment.