Friday, 21 October 2016

UITextFiled Class


//  FormTextField.h


#import <UIKit/UIKit.h>

@interface FormTextField : UITextField


/**
 *  Set left Image
 *
 *  @param img UIImage
 */
- (void)setLeftImage:(UIImage *)img;


/**
 *  Set right Image
 *
 *  @param img UIImage
 */
- (void)setRightImage:(UIImage *)img;
- (void)setPlaceholder:(NSString *)placeholder color:(UIColor *)colorName;
- (void)ForTextfield:(UITextField*)textfieldname setBottomBorder:(int)BorderWidth color:(UIColor*)BorderColor;

@end





//  FormTextField.m


#import "FormTextField.h"
#import "UIColor+CustomColor.h"

@interface FormTextField ()

@property (nonatomic, weak) NSLayoutConstraint *heightConstraint;
@property (nonatomic, weak) NSLayoutConstraint *minHeightConstraint;
@property (nonatomic, weak) NSLayoutConstraint *maxHeightConstraint;


@end

@implementation FormTextField


- (instancetype)initWithCoder:(NSCoder *)aDecoder{
   
    self = [super initWithCoder:aDecoder];
   
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)associateConstraints{
   
    // iterate through all text view's constraints and identify
    // height, max height and min height constraints.
   
    for (NSLayoutConstraint *constraint in self.constraints) {
        if (constraint.firstAttribute == NSLayoutAttributeHeight) {
           
            if (constraint.relation == NSLayoutRelationEqual) {
                self.heightConstraint = constraint;
            }
           
            else if (constraint.relation == NSLayoutRelationLessThanOrEqual) {
                self.maxHeightConstraint = constraint;
            }
           
            else if (constraint.relation == NSLayoutRelationGreaterThanOrEqual) {
                self.minHeightConstraint = constraint;
            }
        }
    }
   
}

/**
 *  Default Initialization
 */
- (void)initialize{
   
    [self associateConstraints];
   
    //    self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
   
   // self.borderStyle        = UITextBorderStyleNone;
    self.leftViewMode       = UITextFieldViewModeAlways;
    self.rightViewMode      = UITextFieldViewModeUnlessEditing;
    self.clearButtonMode    = UITextFieldViewModeWhileEditing;
   
    self.textColor          = [UIColor whiteColor];
    self.tintColor          = [UIColor blackColor];
   // self.backgroundColor    = [UIColor colorWithHexaCode:@"82b9fc"];
   
   
   // self.layer.borderColor  = COLOR_BLUE_LIGHT.CGColor;
//    self.layer.borderWidth  = 2;
//    self.layer.cornerRadius = 3;
//   
    //    self.textAlignment = NSTextAlignmentCenter;
   
    [self setPlaceholder:self.placeholder];
   
   
    int minHeight = 40;
   
    if (DEVICE_IS_IPAD){
       
        //self.font               = [UIFont fontWithName:FONT_ROBOTO_REGULAR size:18];
        minHeight  = 50;

    }else{
      //  self.font               = [UIFont fontWithName:FONT_ROBOTO_REGULAR size:12];
    }
   
   
    if (_heightConstraint) {
       
    }else{
        
        _heightConstraint = [NSLayoutConstraint constraintWithItem:self
                                                         attribute:NSLayoutAttributeHeight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeHeight
                                                        multiplier:0.0f
                                                          constant:minHeight
                             ];
       
        [self addConstraint:_heightConstraint];
       
    }

   
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds {
   
    CGRect textRect = [super rightViewRectForBounds:bounds];
    textRect.origin.x -= (DEVICE_IS_IPAD)?10:5;
    return textRect;
   
}

- (CGRect)leftViewRectForBounds:(CGRect)bounds{
   
    CGRect textRect = [super leftViewRectForBounds:bounds];
    textRect.origin.x += (DEVICE_IS_IPAD)?10:5;
    return textRect;
   
}

- (CGRect)clearButtonRectForBounds:(CGRect)bounds{
   
    CGRect textRect = [super clearButtonRectForBounds:bounds];
    textRect.origin.x -= (DEVICE_IS_IPAD)?10:5;
    return textRect;

}

- (CGRect)textRectForBounds:(CGRect)bounds{
    CGRect textRect = [super textRectForBounds:bounds];
    textRect.origin.x += 0; //for space in textfield
    textRect.size.width -= 10;
   
    return textRect;
   
}

- (CGRect)editingRectForBounds:(CGRect)bounds{
   
    CGRect textRect = [super editingRectForBounds:bounds];
    textRect.origin.x += 10;
    textRect.size.width -= 10;
    return textRect;
   
}

- (void)setPlaceholder:(NSString *)placeholder color:(UIColor *)colorName{
   
   // if (self.placeholder.length > 0  )
    if (placeholder.length > 0  ) {
        NSAttributedString *str = [[NSAttributedString alloc]
                                   initWithString:[NSString stringWithFormat:@"%@",placeholder]
                                   attributes:@{
                                                NSForegroundColorAttributeName :colorName,
                                                NSFontAttributeName: self.font
                                                }];
       
      /*  NSAttributedString *str = [[NSAttributedString alloc]
                                   initWithString:[NSString stringWithFormat:@"%@",placeholder]
                                   attributes:@{
                                                NSForegroundColorAttributeName : colorFromRGBA(255,255,255, 1),
                                                NSFontAttributeName: self.font
                                                }];*/
        self.attributedPlaceholder = str;
       
    }
   
}

- (void)layoutSubviews{
 
    [super layoutSubviews];
  
    if (self.isEditing) {
        self.layer.borderColor  = [UIColor whiteColor].CGColor;
    }else{
        self.layer.borderColor  =[UIColor whiteColor].CGColor;
        //COLOR_BLUE_LIGHT.CGColor;
    }

}

/**
 *  Set left Image
 *
 *  @param img UIImage
 */
- (void)setLeftImage:(UIImage *)img{
   
    UIImageView *imageView = [[ UIImageView alloc] init];
   
    if (DEVICE_IS_IPAD) {
        imageView.frame = CGRectMake(10, 5, 40, self.frame.size.height);
    }else{
        imageView.frame = CGRectMake(10, 5, 30, self.frame.size.height);
       
    }
   
    imageView.image  = img;
    imageView.contentMode = UIViewContentModeCenter;
   
    self.leftView = imageView;

   
}

/**
 *  Set right Image
 *
 *  @param img UIImage
 */
- (void)setRightImage:(UIImage *)img{
 
   
    UIImageView *imageView = [[ UIImageView alloc] init];
   
    if (DEVICE_IS_IPAD) {
        imageView.frame = CGRectMake(10, 5, 40, self.frame.size.height-10);
    }else
    {
        imageView.frame = CGRectMake(self.frame.size.width-50, 0, 15, 15);

    }
   
       imageView.image  = img;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:imageView];
   
    self.rightView = imageView;

}

-(void)ForTextfield:(UITextField*)textfieldname setBottomBorder:(int)BorderWidth color:(UIColor*)BorderColor
{
   

    UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, textfieldname.frame.size.height + 15.0f, textfieldname.frame.size.width, 1)];
    bottomBorder.backgroundColor =BorderColor;
    [textfieldname addSubview:bottomBorder];
    //    CALayer *border = [CALayer layer];
   
}


@end



//Use like this : Example -

- (void)setSubView {
   
    [_tfName ForTextfield:_tfName setBottomBorder:1 color:[UIColor darkGrayColor]];
    [_tfEmail ForTextfield:_tfEmail setBottomBorder:1 color:[UIColor darkGrayColor]];
    [_tfComment ForTextfield:_tfComment setBottomBorder:1 color:[UIColor darkGrayColor]];
  
    NSDictionary *attr = @{
                           CustomBorderMarginBottomAttributeName:@5,
                           };
   
    [_telView drawBottomBorder:1 color:[UIColor darkGrayColor] attributes:attr];

}



No comments:

Post a Comment