diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm index e94b0f99da..711c1fefbc 100644 --- a/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-ios.mm @@ -109,7 +109,8 @@ static const int CC_EDIT_BOX_PADDING = 5; textField.hidden = true; textField.returnKeyType = UIReturnKeyDefault; - [textField addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged]; + [textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged]; + self.textField = textField; self.editBox = editBox; @@ -189,8 +190,13 @@ static const int CC_EDIT_BOX_PADDING = 5; /** * Called each time when the text field's text has changed. */ -- (void)textChanged +- (void)textChanged:(UITextField*)textField { + int maxLength = getEditBoxImplIOS()->getMaxLength(); + if (textField.text.length > maxLength) { + textField.text = [textField.text substringToIndex:maxLength]; + } + cocos2d::ui::EditBoxDelegate *pDelegate = getEditBoxImplIOS()->getDelegate(); if (pDelegate != NULL) { @@ -287,18 +293,25 @@ static const int CC_EDIT_BOX_PADDING = 5; */ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { - if (getEditBoxImplIOS()->getMaxLength() < 0) + int maxLength = getEditBoxImplIOS()->getMaxLength(); + if (maxLength < 0) { return YES; } + // Prevent crashing undo bug http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield + if(range.length + range.location > textField.text.length) + { + return NO; + } + NSUInteger oldLength = textField.text.length; NSUInteger replacementLength = string.length; NSUInteger rangeLength = range.length; NSUInteger newLength = oldLength - rangeLength + replacementLength; - return newLength <= getEditBoxImplIOS()->getMaxLength(); + return newLength <= maxLength; } @end