Skip to content

Commit

Permalink
fix setMaxLength issue of EditBox-mac
Browse files Browse the repository at this point in the history
  • Loading branch information
zilongshanren committed Oct 10, 2014
1 parent 2a95286 commit 2013cdb
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions cocos/ui/UIEditBox/UIEditBoxImpl-mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,60 @@ of this software and associated documentation files (the "Software"), to deal

#define getEditBoxImplMac() ((cocos2d::ui::EditBoxImplMac*)editBox_)

@interface CustomTextFieldFormatter : NSFormatter {
int maxLength;
}
- (void)setMaximumLength:(int)len;
- (int)maximumLength;

@end

@implementation CustomTextFieldFormatter

- (id)init {

if(self = [super init]){

maxLength = INT_MAX;
}

return self;
}

- (void)setMaximumLength:(int)len {
maxLength = len;
}

- (int)maximumLength {
return maxLength;
}

- (NSString *)stringForObjectValue:(id)object {
return (NSString *)object;
}

- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error {
*object = string;
return YES;
}

- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange
errorDescription:(NSString **)error {
if ([*partialStringPtr length] > maxLength) {
return NO;
}

return YES;
}

- (NSAttributedString *)attributedStringForObjectValue:(id)anObject withDefaultAttributes:(NSDictionary *)attributes {
return nil;
}

@end


@implementation UIEditBoxImplMac
Expand Down Expand Up @@ -206,28 +260,6 @@ - (void)controlTextDidEndEditing:(NSNotification *)notification
#endif
}

/**
* Delegate method called before the text has been changed.
* @param textField The text field containing the text.
* @param range The range of characters to be replaced.
* @param string The replacement string.
* @return YES if the specified text range should be replaced; otherwise, NO to keep the old text.
*/
- (BOOL)textField:(NSTextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (getEditBoxImplMac()->getMaxLength() < 0)
{
return YES;
}

NSUInteger oldLength = [[textField stringValue] length];
NSUInteger replacementLength = [string length];
NSUInteger rangeLength = range.length;

NSUInteger newLength = oldLength - rangeLength + replacementLength;

return newLength <= getEditBoxImplMac()->getMaxLength();
}

/**
* Called each time when the text field's text has changed.
Expand Down Expand Up @@ -364,6 +396,10 @@ - (void)controlTextDidChange:(NSNotification *)notification
void EditBoxImplMac::setMaxLength(int maxLength)
{
_maxTextLength = maxLength;
id formater = [[[CustomTextFieldFormatter alloc]init] autorelease];
[formater setMaximumLength:maxLength];
[_sysEdit.secureTextField setFormatter:formater];
[_sysEdit.textField setFormatter:formater];
}

int EditBoxImplMac::getMaxLength()
Expand Down

0 comments on commit 2013cdb

Please sign in to comment.