fix iOS EditBox Chinese input maxLength issue.

https://github.com/cocos2d/cocos2d-x/issues/12519
This commit is contained in:
andyque 2015-08-13 14:58:16 +08:00
parent 5e21580cdd
commit f9385d1604
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