Permalink
Browse files

refactoring in Filters

  • Loading branch information...
Malaar committed May 29, 2012
1 parent 79466c2 commit df0ffe9bbfe2d61ac6307218a276e3546bbf910d
@@ -8,19 +8,19 @@
#import <Foundation/Foundation.h>
#import "MUValidator.h"
+#import "MUFilter.h"
#import "MUKeyboardAvoiderProtocol.h"
-#import "MUInputTextFilter.h"
@class MUTextField_Holder;
@interface MUTextField : UITextField <MUValidationProtocol, UITextFieldDelegate, MUKeyboardAvoiderProtocol>
{
MUValidator* validator;
MUTextField_Holder* delegateHolder;
- MUInputFilter *inputTextFilter;
+ MUFilter *inputTextFilter;
}
@property (nonatomic, assign) id<UITextFieldDelegate> mudelegate;
-@property (nonatomic, retain) MUInputFilter *inputTextFilter;
+@property (nonatomic, retain) MUFilter *filter;
@end
@@ -35,7 +35,7 @@ @implementation MUTextField
@synthesize validatableText;
@synthesize mudelegate;
@synthesize keyboardAvoiding;
-@synthesize inputTextFilter;
+@synthesize filter;
#pragma mark - Init/Dealloc
//==========================================================================================
@@ -189,8 +189,8 @@ - (void)textFieldDidEndEditing:(UITextField *)textField
- (BOOL)textField:(MUTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
BOOL result = YES;
- if (textField.inputTextFilter)
- result = [textField.inputTextFilter filterText:textField shouldChangeCharactersInRange:range replacementString:string];
+ if (textField.filter)
+ result = [textField.filter filterText:textField shouldChangeCharactersInRange:range replacementString:string];
if([holded.mudelegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)])
result = [holded.mudelegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
@@ -9,19 +9,19 @@
#import <Foundation/Foundation.h>
#import "MUValidator.h"
#import "MUKeyboardAvoiderProtocol.h"
-#import "MUInputTextFilter.h"
+#import "MUFilter.h"
@class MUTextView_Holder;
@interface MUTextView : UITextView <MUValidationProtocol, UITextViewDelegate, MUKeyboardAvoiderProtocol>
{
MUValidator* validator;
MUTextView_Holder* delegateHolder;
- MUInputFilter *inputTextFilter;
+ MUFilter *filter;
}
@property (nonatomic, assign) id<UITextViewDelegate> mudelegate;
@property (nonatomic, copy) NSString* observedText;
-@property (nonatomic, retain) MUInputFilter *inputTextFilter;
+@property (nonatomic, retain) MUFilter *filter;
@end
@@ -34,7 +34,7 @@ @implementation MUTextView
@synthesize mudelegate;
@synthesize keyboardAvoiding;
@synthesize observedText;
-@synthesize inputTextFilter;
+@synthesize filter;
//==========================================================================================
- (id) init
@@ -71,7 +71,7 @@ - (void) dealloc
{
[delegateHolder release];
[validator release];
- [inputTextFilter release];
+ [filter release];
[super dealloc];
}
@@ -191,9 +191,9 @@ - (BOOL)textView:(MUTextView *)textView shouldChangeTextInRange:(NSRange)range r
{
BOOL result = YES;
- if (textView.inputTextFilter)
+ if (textView.filter)
{
- result = [textView.inputTextFilter filterText:textView shouldChangeCharactersInRange:range replacementString:text];
+ result = [textView.filter filterText:textView shouldChangeCharactersInRange:range replacementString:text];
}
if([holded.mudelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)])
View
@@ -0,0 +1,40 @@
+//
+// MUInputTextFilter.h
+// MUKitTest
+//
+// Created by Yuriy Bosov on 4/18/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+//==============================================================================
+/// text filter : base class
+@interface MUFilter : NSObject
+{
+ NSUInteger maxLengthText;
+}
+
+@property (nonatomic, assign) NSUInteger maxLengthText;
+
+- (id)initWithMaxLengthText:(NSUInteger) aMaxLengthText;
+- (BOOL) filterText:(id)inputTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
+
+@end
+
+//==============================================================================
+/// text with only number
+@interface MUFilterNumbers : MUFilter
+
+@end
+
+//==============================================================================
+/// text with only letters
+@interface MUFilterLetters : MUFilter
+
+@end
+
+//==============================================================================
+@interface MUFilterLettersAndDigits : MUFilter
+
+@end
View
@@ -0,0 +1,113 @@
+//
+// MUInputTextFilter.m
+// MUKitTest
+//
+// Created by Yuriy Bosov on 4/18/12.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import "MUFilter.h"
+
+//==============================================================================
+//==============================================================================
+//==============================================================================
+/// text filter : base class
+@implementation MUFilter
+
+@synthesize maxLengthText;
+
+//==============================================================================
+- (id) init
+{
+ if( (self = [super init]) )
+ {
+ maxLengthText = NSNotFound;
+ }
+ return self;
+}
+
+//==============================================================================
+- (id)initWithMaxLengthText:(NSUInteger) aMaxLengthText
+{
+ if( (self = [super init]) )
+ {
+ maxLengthText = aMaxLengthText;
+ }
+ return self;
+}
+
+//==============================================================================
+- (BOOL) filterText:(id)inputTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
+{
+// result &= [[inputTextField text] length] < maxLengthText;
+ return YES;
+}
+
+@end
+
+//==============================================================================
+//==============================================================================
+//==============================================================================
+/// text with only number
+@implementation MUFilterNumbers
+
+//==============================================================================
+- (BOOL) filterText:(id)inputTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
+{
+ BOOL result = YES;
+
+ if ([string length] > 0)
+ {
+ result = [string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound;
+ result &= [[inputTextField text] length] < maxLengthText;
+ }
+
+ return result;
+}
+
+@end
+
+//==============================================================================
+//==============================================================================
+//==============================================================================
+/// text with only letters
+@implementation MUFilterLetters
+
+//==============================================================================
+- (BOOL) filterText:(id)inputTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
+{
+ BOOL result = YES;
+
+ if ([string length] > 0)
+ {
+ result = [string rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]].location != NSNotFound;
+ result &= [[inputTextField text] length] < maxLengthText;
+ }
+
+ return result;
+}
+
+
+@end
+
+//==============================================================================
+//==============================================================================
+//==============================================================================
+@implementation MUFilterLettersAndDigits
+
+- (BOOL) filterText:(id)inputTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
+{
+ BOOL result = YES;
+
+ if([string length] > 0)
+ {
+ NSRegularExpression* regExp = [[NSRegularExpression alloc]initWithPattern:@"^[0-9a-zA-Z]+$" options:NSRegularExpressionCaseInsensitive error:nil];
+ NSUInteger count = [regExp numberOfMatchesInString:[inputTextField text] options:0 range:NSMakeRange(0, [inputTextField text].length)];
+ result = count == 1;
+ [regExp release];
+ }
+
+ return result;
+}
+
+@end
@@ -1,72 +0,0 @@
-//
-// MUInputTextFilter.h
-// MUKitTest
-//
-// Created by Yuriy Bosov on 4/18/12.
-// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
-//
-
-#import <Foundation/Foundation.h>
-
-//==============================================================================
-//==============================================================================
-//==============================================================================
-/// text filter : base class
-@interface MUInputFilter : NSObject
-
-- (BOOL) filterText:(id)inputTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
-
-@end
-
-//==============================================================================
-//==============================================================================
-//==============================================================================
-/// text with only number
-@interface MUInputFilterNumbreValue : MUInputFilter
-
-@end
-
-//==============================================================================
-//==============================================================================
-//==============================================================================
-/// text with only numbers, numbers value has max
-@interface MUInputFilterNumbreValueWithMaxLengthText : MUInputFilterNumbreValue
-
-@property (nonatomic, assign) NSUInteger maxLengthText;
-
-- (id)initWithMaxLengthText:(NSUInteger) aMaxLengthText;
-
-@end
-
-//==============================================================================
-//==============================================================================
-//==============================================================================
-/// text with only letters
-@interface MUInputFilterLetterValue : MUInputFilter
-
-@end
-
-//==============================================================================
-//==============================================================================
-//==============================================================================
-/// text with only letters, text has limited the length
-@interface MUInputFilterLetterValueWithMaxLengthText : MUInputFilterLetterValue
-
-@property (nonatomic, assign) NSUInteger maxLengthText;
-
-- (id)initWithMaxLengthText:(NSUInteger) aMaxLengthText;
-
-@end
-
-//==============================================================================
-//==============================================================================
-//==============================================================================
-/// text has limited the length
-@interface MUInputFilterStringWithMaxLengthText : MUInputFilter
-
-@property (nonatomic, assign) NSUInteger maxLengthText;
-
-- (id)initWithMaxLengthText:(NSUInteger) aMaxLengthText;
-
-@end
-
Oops, something went wrong.

0 comments on commit df0ffe9

Please sign in to comment.