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

Fixed issue #38 with custom keyboards #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
38 changes: 26 additions & 12 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,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];
}
Expand Down Expand Up @@ -473,7 +474,7 @@ - (void)unhighlightAllTokens
for (VENToken *token in self.tokens) {
token.highlighted = NO;
}

[self setCursorVisibility];
}

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

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

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

return 0;
}

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

return @"";
}

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

return NO;
}

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

Expand Down