1 // 2 // SXTTextField.h 3 // 04-UITextField练习 4 // 5 // Created by andezhou on 16/1/8. 6 // Copyright (c) 2016年 周安德. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface SXTTextField : UITextField 12 13 // 设置左边图片名字 14 @property (copy, nonatomic) NSString *leftImgName; 15 16 /** 17 * @brief 构造方法 18 * 19 * @param frame 位置宽高 20 * @param imgName 21 * 22 */ 23 - (instancetype)initWithFrame:(CGRect)frame 24 leftImgName:(NSString *)leftImgName; 25 26 @end
/************************************************/
1 // 2 // SXTTextField.m 3 // 04-UITextField练习 4 // 5 // Created by andezhou on 16/1/8. 6 // Copyright (c) 2016年 周安德. All rights reserved. 7 // 8 9 #import "SXTTextField.h" 10 11 @interface SXTTextField () 12 13 @property (strong, nonatomic) UIImageView *leftImageView; 14 15 @end 16 17 @implementation SXTTextField 18 19 #pragma mark - 20 #pragma mark lifecycle 21 - (instancetype)initWithFrame:(CGRect)frame 22 leftImgName:(NSString *)leftImgName 23 { 24 self.leftImgName = leftImgName; 25 26 return [self initWithFrame:frame]; 27 } 28 29 - (instancetype)initWithFrame:(CGRect)frame 30 { 31 if (self = [super initWithFrame:frame]) { 32 // 设置xx 33 self.clearButtonMode = UITextFieldViewModeAlways; 34 // 第二次输入清除内容 35 self.clearsOnBeginEditing = YES; 36 37 // return模式 38 self.returnKeyType = UIReturnKeyDone; 39 40 // 设置背景图片 41 self.background = [UIImage imageNamed:@"background"]; 42 43 // 设置左边view 44 self.leftView = self.leftImageView; 45 // 设置左边view一直存在 46 self.leftViewMode = UITextFieldViewModeAlways; 47 } 48 return self; 49 } 50 51 #pragma mark - 52 #pragma mark init methods 53 - (UIImageView *)leftImageView 54 { 55 if (!_leftImageView) { 56 _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 45, 36)]; 57 _leftImageView.backgroundColor = [UIColor clearColor]; 58 _leftImageView.contentMode = UIViewContentModeCenter; 59 } 60 return _leftImageView; 61 } 62 63 #pragma mark - 64 #pragma mark set 65 - (void)setLeftImgName:(NSString *)leftImgName 66 { 67 _leftImgName = leftImgName; 68 69 // 把图片展示在leftView上面 70 self.leftImageView.image = [UIImage imageNamed:leftImgName]; 71 } 72 73 @end
/***********************************************************/
1 // 2 // UIColor+Extension.h 3 // 九宫格 4 // 5 // Created by andezhou on 16/1/3. 6 // Copyright © 2016年 周安德. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface UIColor (Extension) 12 13 + (UIColor *)colorWithHexString:(NSString *)color; 14 + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha; 15 16 17 @end
/*************************************************************/
1 // 2 // UIColor+Extension.m 3 // 九宫格 4 // 5 // Created by andezhou on 16/1/3. 6 // Copyright © 2016年 周安德. All rights reserved. 7 // 8 9 #import "UIColor+Extension.h" 10 #define DEFAULT_VOID_COLOR [UIColor whiteColor] 11 12 @implementation UIColor (Extension) 13 14 // #000000 15 + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha 16 { 17 NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; 18 19 if ([cString length] < 6) 20 return DEFAULT_VOID_COLOR; 21 if ([cString hasPrefix:@"#"]) 22 cString = [cString substringFromIndex:1]; 23 if ([cString length] != 6) 24 return DEFAULT_VOID_COLOR; 25 26 NSRange range; 27 range.location = 0; 28 range.length = 2; 29 NSString *rString = [cString substringWithRange:range]; 30 31 range.location = 2; 32 NSString *gString = [cString substringWithRange:range]; 33 34 range.location = 4; 35 NSString *bString = [cString substringWithRange:range]; 36 37 38 unsigned int r, g, b; 39 [[NSScanner scannerWithString:rString] scanHexInt:&r]; 40 [[NSScanner scannerWithString:gString] scanHexInt:&g]; 41 [[NSScanner scannerWithString:bString] scanHexInt:&b]; 42 43 return [UIColor colorWithRed:((float) r / 255.0f) 44 green:((float) g / 255.0f) 45 blue:((float) b / 255.0f) 46 alpha:alpha]; 47 } 48 49 + (UIColor *)colorWithHexString:(NSString *)color 50 { 51 return [UIColor colorWithHexString:color alpha:1.0]; 52 } 53 @end
/**************************************************/
1 // 2 // ViewController.m 3 // 04-UITextField练习 4 // 5 // Created by andezhou on 16/1/8. 6 // Copyright (c) 2016年 周安德. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "UIColor+Extension.h" 11 #import "SXTTextField.h" 12 13 static NSUInteger kMargin = 20; 14 #define kTextFieldWidth [UIScreen mainScreen].bounds.size.width - 2*kMargin 15 16 @interface ViewController () <UITextFieldDelegate> 17 18 @property (strong, nonatomic) SXTTextField *userNameTextField, *passwordTextField; 19 @property (strong, nonatomic) UIButton *loginBtn; 20 21 @end 22 23 @implementation ViewController 24 25 26 - (SXTTextField *)userNameTextField 27 { 28 if (!_userNameTextField) { 29 _userNameTextField = [[SXTTextField alloc] initWithFrame:CGRectMake(kMargin, 80, kTextFieldWidth, 44)]; 30 _userNameTextField.delegate = self; 31 _userNameTextField.leftImgName = @"userName"; 32 _userNameTextField.placeholder = @"请输入手机号"; 33 _userNameTextField.keyboardType = UIKeyboardTypeNumberPad; 34 } 35 return _userNameTextField; 36 } 37 38 - (SXTTextField *)passwordTextField 39 { 40 if (!_passwordTextField) { 41 CGRect frame = CGRectMake(kMargin, CGRectGetMaxY(_userNameTextField.frame) + 30, kTextFieldWidth, 44); 42 _passwordTextField = [[SXTTextField alloc] initWithFrame:frame leftImgName:@"password"]; 43 _passwordTextField.delegate = self; 44 // 密码模式 45 _passwordTextField.secureTextEntry = YES; 46 _passwordTextField.placeholder = @"请输入密码"; 47 } 48 return _passwordTextField; 49 } 50 51 - (UIButton *)loginBtn 52 { 53 if (!_loginBtn) { 54 CGFloat pointY = CGRectGetMaxY(_passwordTextField.frame) + 50; 55 _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 56 _loginBtn.frame = CGRectMake(kMargin, pointY, kTextFieldWidth, 44); 57 [_loginBtn setTitle:@"登录" forState:UIControlStateNormal]; 58 _loginBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16]; 59 [_loginBtn setBackgroundImage:[UIImage imageNamed:@"beijing"] forState:UIControlStateNormal]; 60 [_loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside]; 61 _loginBtn.layer.cornerRadius = 4.0f; 62 _loginBtn.layer.masksToBounds = YES; 63 _loginBtn.enabled = NO; 64 } 65 return _loginBtn; 66 } 67 68 #pragma mark - 69 #pragma mark loginAction 70 - (void)loginAction:(UIButton *)btn 71 { 72 NSLog(@"userName:%@ password:%@", _userNameTextField.text, _passwordTextField.text); 73 } 74 75 #pragma mark - 76 #pragma mark UITextFieldDelegate 77 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 78 { 79 if ([_passwordTextField.text isEqualToString:@""] || [_userNameTextField.text isEqualToString:@""]) { 80 _loginBtn.enabled = NO; 81 }else{ 82 _loginBtn.enabled = YES; 83 } 84 85 return YES; 86 } 87 88 - (BOOL)textFieldShouldReturn:(UITextField *)textField 89 { 90 [self.view endEditing:YES]; 91 return YES; 92 } 93 94 #pragma mark - 95 #pragma mark lifecycle 96 - (void)viewDidLoad { 97 [super viewDidLoad]; 98 self.view.backgroundColor = [UIColor colorWithHexString:@"#f7f8f3"]; 99 [self.view addSubview:self.userNameTextField]; 100 [self.view addSubview:self.passwordTextField]; 101 [self.view addSubview:self.loginBtn]; 102 103 } 104 105 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 106 { 107 [self.view endEditing:YES]; 108 } 109 110 - (void)didReceiveMemoryWarning { 111 [super didReceiveMemoryWarning]; 112 // Dispose of any resources that can be recreated. 113 } 114 115 @end
/***************************************************************/