25 Mar 2017

UITextField Delegate Methods & Giving Some Restrictions while filling text into it...


UITextField Delegate Methods & Giving Some Restrictions while filling text into it...


#import <UIKit/UIKit.h>

@interface viewController : UIViewController<UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UITextField *firstName;
@property (weak, nonatomic) IBOutlet UITextField *lastName;
@property (weak, nonatomic) IBOutlet UITextField *homeTown;
@property (weak, nonatomic) IBOutlet UITextField *state;
@property (weak, nonatomic) IBOutlet UITextField *age;
@property (weak, nonatomic) IBOutlet UILabel *ageAlertLabel;

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;

@property (weak, nonatomic) IBOutlet UITextField *companyName;
@property (weak, nonatomic) IBOutlet UITextView *companyAddress;
@property (weak, nonatomic) IBOutlet UITextField *website;

@property (weak, nonatomic) IBOutlet UITextField *mobileNumber;
@property (weak, nonatomic) IBOutlet UITextField *facebookID;
@property (weak, nonatomic) IBOutlet UITextField *twitterID;
@property (weak, nonatomic) IBOutlet UITextField *emailID;

@end



#import "viewController1.h"

@interface viewController ()

@end

@implementation viewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.textColor=[UIColor blueColor];
    textField.textAlignment=1;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
{
    if(textField==self.firstName||textField==self.lastName||textField==self.homeTown||textField==self.state||textField==self.age)
        {
        if(textField.text.length>0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    return NO;
}

- (void)textFieldDidEndEditing:(UITextField *)textField;
{
    textField.textColor=[UIColor redColor];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == self.firstName||textField==self.lastName||textField==self.homeTown||textField==self.state)
    {
        NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];
        for (int i=0; i<[string length]; i++)
        {
            unichar c=[string characterAtIndex:i];
            if(![charSet characterIsMember:c])
            {
                return NO;
            }
        }
        return YES;
    }

    else if(textField == self.age)
    {
        NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        for (int i=0; i<[string length]; i++)
        {
            unichar c=[string characterAtIndex:i];
            if(![charSet characterIsMember:c])
            {
                return NO;
            }
            if(self.age.text.length>2)
            {
                return NO;
            }
        }
        return YES;
    }

    else if(textField==self.companyName)
    {
        NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];
        for (int i=0; i<[string length]; i++)
        {
            unichar c=[string characterAtIndex:i];
            if(![charSet characterIsMember:c])
            {
                return NO;
            }
        }
        return YES;
    }
    else if(textField == self.website)
    {
        NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.0123456789"];
        for (int i=0; i<[string length]; i++)
        {
            unichar c=[string characterAtIndex:i];
            if(![charSet characterIsMember:c])
            {
                return NO;
            }
        }
        return YES;
    }

     else if(textField == self.mobileNumber)
    {
        NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789+"];
        for (int i=0; i<[string length]; i++)
        {
            unichar c=[string characterAtIndex:i];
            if(![charSet characterIsMember:c])
            {
                return NO;
            }
            if(self.mobileNumber.text.length>12)
            {
                return NO;
            }
        }
    }

    return YES;
}

@end

No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0