Merge pull request #13352 from zilongshanren/fixIssue12519

fix iOS EditBox Chinese input maxLength issue.
This commit is contained in:
子龙山人 2015-08-13 17:04:19 +08:00
commit aa7b96bb40
1 changed files with 17 additions and 4 deletions

View File

@ -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