Showing posts with label TextField related.... Show all posts
Showing posts with label TextField related.... Show all posts

16 Mar 2018

How to give a Bottom line to a TextField iOS...

How to give a Bottom line to a TextField iOS...

To give a underline to textField (Only bottom border) line use the following code:



Here we use CALayer class to achieve it,

-(void)underlineToTextField:(UITextField *)txtField
{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 0.8f;
    border.borderColor = [UIcolor greenColor].CGColor;
    border.frame = CGRectMake(0, text.frame.size.height -      borderWidth, ([[UIScreen mainScreen] bounds].size.width)-32, text.frame.size.height);
    border.borderWidth = borderWidth;
    [text.layer addSublayer:border];
    text.layer.masksToBounds = YES;
}

Call the method for which TextField you want to show underline like below,

[self underlineToTextField:textField];



12 Aug 2017

How to animate textField on the top of Keyboard while Editing...



How to animate textField on the top of Keyboard while Editing...



#pragma  – mark Textfield Delegate Methods..

-(void)textFieldDidBeginEditing:(UITextField *)textField  {
    
     [self animateTextField:textField up:YES withOffset:textField.frame.origin.y / 2];
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
    
    [self animateTextField:textField up:NO withOffset:textField.frame.origin.y / 2];

}


#pragma  – mark Textfield Animation Method

-(void)animateTextField:(UITextField*)textField up:(BOOL)up withOffset:(CGFloat)offset
{
    const int movementDistance = -offset;
    const float movementDuration = 0.4f;
    int movement = (up ? movementDistance : -movementDistance);
    [UIView beginAnimations: @"animateTextField" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];

}

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

Recent Posts

Codable demo

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