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 ada3790
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions VENTokenField/VENTokenField.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ - (void)setUpInit

// Accessing bare value to avoid kicking off a premature layout run.
_toLabelText = NSLocalizedString(@"To:", nil);

self.originalHeight = CGRectGetHeight(self.frame);

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

[self layoutScrollView];
[self reloadData];
}
Expand Down Expand Up @@ -187,11 +187,11 @@ - (void)layoutCollapsedLabel
[self.collapsedLabel removeFromSuperview];
self.scrollView.hidden = YES;
[self setHeight:self.originalHeight];

CGFloat currentX = 0;
[self layoutToLabelInView:self origin:CGPointMake(self.horizontalInset, self.verticalInset) currentX:&currentX];
[self layoutCollapsedLabelWithCurrentX:&currentX];

self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self addGestureRecognizer:self.tapGestureRecognizer];
Expand All @@ -204,24 +204,24 @@ - (void)layoutTokensAndInputWithFrameAdjustment:(BOOL)shouldAdjustFrame
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
self.scrollView.hidden = NO;
[self removeGestureRecognizer:self.tapGestureRecognizer];

self.tokens = [NSMutableArray array];

CGFloat currentX = 0;
CGFloat currentY = 0;

[self layoutToLabelInView:self.scrollView origin:CGPointZero currentX:&currentX];
[self layoutTokensWithCurrentX:&currentX currentY:&currentY];
[self layoutInputTextFieldWithCurrentX:&currentX currentY:&currentY clearInput:shouldAdjustFrame];

if (shouldAdjustFrame) {
[self adjustHeightForCurrentY:currentY];
}

[self.scrollView setContentSize:CGSizeMake(self.scrollView.contentSize.width, currentY + [self heightForToken])];

[self updateInputTextField];

if (inputFieldShouldBecomeFirstResponder) {
[self inputTextFieldBecomeFirstResponder];
} else {
Expand All @@ -244,7 +244,7 @@ - (void)layoutScrollView
self.verticalInset,
self.horizontalInset);
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

[self addSubview:self.scrollView];
}

Expand All @@ -256,7 +256,7 @@ - (void)layoutInputTextFieldWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *
*currentY += [self heightForToken];
*currentX = 0;
}

VENBackspaceTextField *inputTextField = self.inputTextField;
if (clearInput) {
inputTextField.text = @"";
Expand Down Expand Up @@ -300,18 +300,18 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current
for (NSUInteger i = 0; i < [self numberOfTokens]; i++) {
NSString *title = [self titleForTokenAtIndex:i];
VENToken *token = [[VENToken alloc] init];

__weak VENToken *weakToken = token;
__weak VENTokenField *weakSelf = self;
token.didTapTokenBlock = ^{
[weakSelf didTapToken:weakToken];
};

[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
token.frame = CGRectMake(*currentX, *currentY, token.width, token.height);
} else {
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 All @@ -350,7 +350,7 @@ - (void)inputTextFieldBecomeFirstResponder
if (self.inputTextField.isFirstResponder) {
return;
}

[self.inputTextField becomeFirstResponder];
if ([self.delegate respondsToSelector:@selector(tokenFieldDidBeginEditing:)]) {
[self.delegate tokenFieldDidBeginEditing:self];
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 @@ -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 ada3790

Please sign in to comment.