@interface FormTextField : UITextField
-
(void)setLeftImage:(UIImage *)img;
-
(void)setRightImage:(UIImage *)img;
-
(void)setPlaceholder:(NSString *)placeholder color:(UIColor *)colorName;
-
(void)ForTextfield:(UITextField*)textfieldname setBottomBorder:(int)BorderWidth color:(UIColor*)BorderColor;
#import
"FormTextField.h"
#import "UIColor+CustomColor.h"
@interface FormTextField ()
@property (nonatomic,
weak) NSLayoutConstraint *heightConstraint;
@property (nonatomic,
weak) NSLayoutConstraint *minHeightConstraint;
@property (nonatomic,
weak) NSLayoutConstraint *maxHeightConstraint;
@implementation FormTextField
-
(instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
-
(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;
[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];
//self.font = [UIFont
fontWithName:FONT_ROBOTO_REGULAR size:18];
// self.font = [UIFont
fontWithName:FONT_ROBOTO_REGULAR size:12];
_heightConstraint = [NSLayoutConstraint
constraintWithItem:self
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
attribute:NSLayoutAttributeHeight
[self addConstraint:_heightConstraint];
-
(CGRect)rightViewRectForBounds:(CGRect)bounds {
CGRect textRect = [super rightViewRectForBounds:bounds];
textRect.origin.x -=
(DEVICE_IS_IPAD)?10:5;
-
(CGRect)leftViewRectForBounds:(CGRect)bounds{
CGRect textRect = [super leftViewRectForBounds:bounds];
textRect.origin.x +=
(DEVICE_IS_IPAD)?10:5;
-
(CGRect)clearButtonRectForBounds:(CGRect)bounds{
CGRect textRect = [super clearButtonRectForBounds:bounds];
textRect.origin.x -=
(DEVICE_IS_IPAD)?10:5;
-
(CGRect)textRectForBounds:(CGRect)bounds{
CGRect textRect = [super textRectForBounds:bounds];
textRect.origin.x += 0; //for space in textfield
textRect.size.width -= 10;
-
(CGRect)editingRectForBounds:(CGRect)bounds{
CGRect textRect = [super editingRectForBounds:bounds];
textRect.size.width -= 10;
-
(void)setPlaceholder:(NSString *)placeholder color:(UIColor *)colorName{
//
if (self.placeholder.length > 0 )
if (placeholder.length > 0 ) {
NSAttributedString *str = [[NSAttributedString alloc]
initWithString:[NSString stringWithFormat:@"%@",placeholder]
NSForegroundColorAttributeName :colorName,
NSFontAttributeName: self.font
/* NSAttributedString *str =
[[NSAttributedString alloc]
initWithString:[NSString stringWithFormat:@"%@",placeholder]
NSForegroundColorAttributeName
: colorFromRGBA(255,255,255, 1),
NSFontAttributeName: self.font
self.attributedPlaceholder = str;
self.layer.borderColor =
[UIColor
whiteColor].CGColor;
self.layer.borderColor =[UIColor whiteColor].CGColor;
//COLOR_BLUE_LIGHT.CGColor;
-
(void)setLeftImage:(UIImage *)img{
UIImageView *imageView = [[ UIImageView alloc] init];
imageView.frame = CGRectMake(10, 5, 40, self.frame.size.height);
imageView.frame = CGRectMake(10, 5, 30, self.frame.size.height);
imageView.contentMode =
UIViewContentModeCenter;
self.leftView = imageView;
-
(void)setRightImage:(UIImage *)img{
UIImageView *imageView = [[ UIImageView alloc] init];
imageView.frame = CGRectMake(10, 5, 40, self.frame.size.height-10);
imageView.frame = CGRectMake(self.frame.size.width-50, 0, 15, 15);
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];
//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]];
CustomBorderMarginBottomAttributeName:@5,
[_telView drawBottomBorder:1 color:[UIColor darkGrayColor] attributes:attr];