4

I have so many UITextField in my App.

I don't want to allow user to enter special characters those textfields.

I know, I can use shouldChangeCharactersInRange delegate method of UITextFiled and validate it, But this approach is feasible for 5-8 UITextFiled not for 15-20.

I want to validate those(15-20) UITextFileds using RuntimeAttributes and UICategory as below links:-

http://johannesluderschmidt.de/category-for-setting-maximum-length-of-text-in-uitextfields-on-ios-using-objective-c/3209/

http://spin.atomicobject.com/2014/05/30/xcode-runtime-attributes/

I tried and create UICategory of UITextFiled as below:-

UITextField+RunTimeExtension.h

@interface UITextField (RunTimeExtension)

@property(nonatomic,assign) BOOL *isAllowedSpecialCharacters;

@end

UITextField+RunTimeExtension.m

-(void)setIsAllowedSpecialCharacters:(BOOL *)isAllowedSpecialCharacters{

-(BOOL)isIsAllowedSpecialCharacters{
    if(self.isAllowedSpecialCharacters){
        NSCharacterSet *characterSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];

        NSString *filtered = [[self.text componentsSeparatedByCharactersInSet:characterSet]  componentsJoinedByString:@""];

        return [self.text isEqualToString:filtered] || [self.text isEqualToString:@" "];
    }else{
        return NO;
    }
}

and add this attribute in RuntimeAttribute as below image:

Runtime Attribute

But didn't work, if this property is checked or not.

halfer
  • 19,824
  • 17
  • 99
  • 186
Er.Shreyansh Shah
  • 1,482
  • 1
  • 9
  • 29

3 Answers3

1

Why not create custom textfield MyCustomTextField extends UITextField and use this custom textfield wherever needed?

Let me know if you need more details.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
1

There are many mistakes in your code. See my answer for corrections.

UITextField+SpecialCharacters.h

#import <UIKit/UIKit.h>

@interface UITextField (SpecialCharacters)

@property(nonatomic,assign) NSNumber *allowSpecialCharacters;
//here you were using BOOL *

@end

UITextField+SpecialCharacters.m

#import "UITextField+SpecialCharacters.h"
#import <objc/runtime.h>

@implementation UITextField (SpecialCharacters)

static void *specialCharKey;

-(void) setAllowSpecialCharacters:(NSNumber *)allowSpecialCharacters{

    objc_setAssociatedObject(self, &specialCharKey, allowSpecialCharacters, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

-(NSNumber *) allowSpecialCharacters{

    return objc_getAssociatedObject(self, &specialCharKey);
}

@end

Always set the names for getter and setter as per the standards.

In your ViewController, set delegate for textfield and implement following delegate method as per your requirement:

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([textField.allowSpecialCharacters boolValue]){
        NSCharacterSet *characterSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];

        NSString *filtered = [[textField.text componentsSeparatedByCharactersInSet:characterSet]  componentsJoinedByString:@""];

        return [textField.text isEqualToString:filtered] || [textField.text isEqualToString:@" "];
    }else{
        return NO;
    }
}

In storyboard/nib, you should set runtime attribute as in snapshot. You can set value to 1 or 0 as per your requirement.

enter image description here

This is working fine at my side. Hope it solves your problem. Thanks.

NightFury
  • 13,436
  • 6
  • 71
  • 120
0

You can use below custom class to meet all your requirements. It uses regular expressions to validate textField you can edit them to handle shouldChangeCharactersInRange according to your regular expression.

https://github.com/tomkowz/TSValidatedTextField

Zahid
  • 552
  • 3
  • 11