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

[TIMOB-10222] iOS: Implement maxLength for TextArea #2670

Merged
merged 4 commits into from
Aug 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions apidoc/Titanium/UI/TextArea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ properties:
or [LINKIFY_WEB_URLS](Titanium.UI.Android.LINKIFY_WEB_URLS).
type: Number
default: <Titanium.UI.iOS.AUTODETECT_NONE> in iOS, undefined in Android
since: {android: 2.2.0}
since: {android: "2.2.0"}
platforms: [android, iphone, ipad]

- name: clearOnEdit
Expand Down Expand Up @@ -193,8 +193,8 @@ properties:
indicates unlimited length.
default: -1
type: Number
since: {android: "2.2.0"}
platforms: [android]
since: {android: "2.2.0", iphone: "2.2.0", ipad: "2.2.0"}
platforms: [android, iphone, ipad]

- name: returnKeyType
summary: |
Expand Down
5 changes: 5 additions & 0 deletions iphone/Classes/TiUITextArea.m
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ - (BOOL)textView:(UITextView *)tv shouldChangeTextInRange:(NSRange)range replace
}
}

if ( (maxLength > -1) && ([curText length] > maxLength) ) {
[self setValue_:curText];
return NO;
}

[(TiUITextAreaProxy *)self.proxy noteValueChange:curText];
return TRUE;
}
Expand Down
1 change: 1 addition & 0 deletions iphone/Classes/TiUITextAreaProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @implementation TiUITextAreaProxy
#pragma mark Defaults

DEFINE_DEF_PROP(value,@"");
DEFINE_DEF_INT_PROP(maxLength,-1);

@end

Expand Down
2 changes: 0 additions & 2 deletions iphone/Classes/TiUITextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
UIView *rightView;

BOOL becameResponder;
NSInteger maxLength;
TiUIView * touchHandler;
}

@property(nonatomic,readwrite,assign) CGFloat paddingLeft;
@property(nonatomic,readwrite,assign) CGFloat paddingRight;
@property(nonatomic,readwrite,assign) CGFloat leftButtonPadding;
@property(nonatomic,readwrite,assign) CGFloat rightButtonPadding;
@property(nonatomic,readwrite,assign) NSInteger maxLength;

@property(nonatomic,readonly) BOOL becameResponder;

Expand Down
23 changes: 2 additions & 21 deletions iphone/Classes/TiUITextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@implementation TiTextField

@synthesize leftButtonPadding, rightButtonPadding, paddingLeft, paddingRight, becameResponder, maxLength;
@synthesize leftButtonPadding, rightButtonPadding, paddingLeft, paddingRight, becameResponder;

-(void)configure
{
Expand All @@ -27,7 +27,6 @@ -(void)configure
rightButtonPadding = 0;
paddingLeft = 0;
paddingRight = 0;
maxLength = -1;
[super setLeftViewMode:UITextFieldViewModeAlways];
[super setRightViewMode:UITextFieldViewModeAlways];
}
Expand Down Expand Up @@ -455,24 +454,6 @@ -(void)setVerticalAlign_:(id)value
}
}

-(void)setValue_:(id)value
{
NSString* string = [TiUtils stringValue:value];
NSInteger maxLength = [[self textWidgetView] maxLength];
if (maxLength > -1 && [string length] > maxLength) {
string = [string substringToIndex:maxLength];
}
[super setValue_:string];
}

-(void)setMaxLength_:(id)value
{
NSInteger maxLength = [TiUtils intValue:value def:-1];
[[self textWidgetView] setMaxLength:maxLength];
[self setValue_:[[self textWidgetView] text]];
[[self proxy] replaceValue:value forKey:@"maxLength" notification:NO];
}

#pragma mark Public Method

-(BOOL)hasText
Expand Down Expand Up @@ -501,8 +482,8 @@ - (BOOL)textField:(UITextField *)tf shouldChangeCharactersInRange:(NSRange)range
{
NSString *curText = [[tf text] stringByReplacingCharactersInRange:range withString:string];

NSInteger maxLength = [[self textWidgetView] maxLength];
if ( (maxLength > -1) && ([curText length] > maxLength) ) {
[self setValue_:curText];
return NO;
}

Expand Down
1 change: 1 addition & 0 deletions iphone/Classes/TiUITextFieldProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ @implementation TiUITextFieldProxy
DEFINE_DEF_INT_PROP(rightButtonMode,UITextFieldViewModeNever);
DEFINE_DEF_INT_PROP(appearance,UIKeyboardAppearanceDefault);
DEFINE_DEF_INT_PROP(autocapitalization,UITextAutocapitalizationTypeNone);
DEFINE_DEF_INT_PROP(maxLength,-1);

@end

Expand Down
3 changes: 2 additions & 1 deletion iphone/Classes/TiUITextWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
@protected
UIView<UITextInputTraits>* textWidgetView;
BOOL suppressReturn;

NSInteger maxLength;

TiUIView<TiScrolling> * parentScrollView;
@private

Expand Down
23 changes: 18 additions & 5 deletions iphone/Classes/TiUITextWidget.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,30 @@ - (id) init
if (self != nil)
{
suppressReturn = YES;
maxLength = -1;
[self textWidgetView];
}
return self;
}


-(void)setValue_:(id)value
{
NSString* string = [TiUtils stringValue:value];
if (maxLength > -1 && [string length] > maxLength) {
string = [string substringToIndex:maxLength];
}
[(id)[self textWidgetView] setText:string];
[(TiUITextWidgetProxy*)[self proxy] noteValueChange:string];
}

-(void)setMaxLength_:(id)value
{
maxLength = [TiUtils intValue:value def:-1];
[self setValue_:[[self proxy] valueForUndefinedKey:@"value"]];
[[self proxy] replaceValue:value forKey:@"maxLength" notification:NO];
}

-(void)setSuppressReturn_:(id)value
{
suppressReturn = [TiUtils boolValue:value def:YES];
Expand Down Expand Up @@ -146,11 +164,6 @@ -(void)setAutocapitalization_:(id)value
[[self textWidgetView] setAutocapitalizationType:[TiUtils intValue:value]];
}

-(void)setValue_:(id)text
{
[(id)[self textWidgetView] setText:[TiUtils stringValue:text]];
}

#pragma mark Keyboard Delegates

-(void)textWidget:(UIView<UITextInputTraits>*)tw didFocusWithText:(NSString *)value
Expand Down