标签:
//
// SXTTextField.h
// 04-UITextField练习
//
// Created by andezhou on 16/1/8.
// Copyright (c) 2016年 周安德. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SXTTextField : UITextField
// 设置左边图片名字
@property (copy, nonatomic) NSString *leftImgName;
/**
* @brief 构造方法
*
* @param frame 位置宽高
* @param imgName
*
*/
- (instancetype)initWithFrame:(CGRect)frame
leftImgName:(NSString *)leftImgName;
@end
/************************************************/
//
// SXTTextField.m
// 04-UITextField练习
//
// Created by andezhou on 16/1/8.
// Copyright (c) 2016年 周安德. All rights reserved.
//
#import "SXTTextField.h"
@interface SXTTextField ()
@property (strong, nonatomic) UIImageView *leftImageView;
@end
@implementation SXTTextField
#pragma mark -
#pragma mark lifecycle
- (instancetype)initWithFrame:(CGRect)frame
leftImgName:(NSString *)leftImgName
{
self.leftImgName = leftImgName;
return [self initWithFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 设置xx
self.clearButtonMode = UITextFieldViewModeAlways;
// 第二次输入清除内容
self.clearsOnBeginEditing = YES;
// return模式
self.returnKeyType = UIReturnKeyDone;
// 设置背景图片
self.background = [UIImage imageNamed:@"background"];
// 设置左边view
self.leftView = self.leftImageView;
// 设置左边view一直存在
self.leftViewMode = UITextFieldViewModeAlways;
}
return self;
}
#pragma mark -
#pragma mark init methods
- (UIImageView *)leftImageView
{
if (!_leftImageView) {
_leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 45, 36)];
_leftImageView.backgroundColor = [UIColor clearColor];
_leftImageView.contentMode = UIViewContentModeCenter;
}
return _leftImageView;
}
#pragma mark -
#pragma mark set
- (void)setLeftImgName:(NSString *)leftImgName
{
_leftImgName = leftImgName;
// 把图片展示在leftView上面
self.leftImageView.image = [UIImage imageNamed:leftImgName];
}
@end
/***********************************************************/
//
// UIColor+Extension.h
// 九宫格
//
// Created by andezhou on 16/1/3.
// Copyright © 2016年 周安德. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIColor (Extension)
+ (UIColor *)colorWithHexString:(NSString *)color;
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;
@end
/*************************************************************/
//
// UIColor+Extension.m
// 九宫格
//
// Created by andezhou on 16/1/3.
// Copyright © 2016年 周安德. All rights reserved.
//
#import "UIColor+Extension.h"
#define DEFAULT_VOID_COLOR [UIColor whiteColor]
@implementation UIColor (Extension)
// #000000
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([cString length] < 6)
return DEFAULT_VOID_COLOR;
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return DEFAULT_VOID_COLOR;
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:alpha];
}
+ (UIColor *)colorWithHexString:(NSString *)color
{
return [UIColor colorWithHexString:color alpha:1.0];
}
@end
/**************************************************/
//
// ViewController.m
// 04-UITextField练习
//
// Created by andezhou on 16/1/8.
// Copyright (c) 2016年 周安德. All rights reserved.
//
#import "ViewController.h"
#import "UIColor+Extension.h"
#import "SXTTextField.h"
static NSUInteger kMargin = 20;
#define kTextFieldWidth [UIScreen mainScreen].bounds.size.width - 2*kMargin
@interface ViewController () <UITextFieldDelegate>
@property (strong, nonatomic) SXTTextField *userNameTextField, *passwordTextField;
@property (strong, nonatomic) UIButton *loginBtn;
@end
@implementation ViewController
- (SXTTextField *)userNameTextField
{
if (!_userNameTextField) {
_userNameTextField = [[SXTTextField alloc] initWithFrame:CGRectMake(kMargin, 80, kTextFieldWidth, 44)];
_userNameTextField.delegate = self;
_userNameTextField.leftImgName = @"userName";
_userNameTextField.placeholder = @"请输入手机号";
_userNameTextField.keyboardType = UIKeyboardTypeNumberPad;
}
return _userNameTextField;
}
- (SXTTextField *)passwordTextField
{
if (!_passwordTextField) {
CGRect frame = CGRectMake(kMargin, CGRectGetMaxY(_userNameTextField.frame) + 30, kTextFieldWidth, 44);
_passwordTextField = [[SXTTextField alloc] initWithFrame:frame leftImgName:@"password"];
_passwordTextField.delegate = self;
// 密码模式
_passwordTextField.secureTextEntry = YES;
_passwordTextField.placeholder = @"请输入密码";
}
return _passwordTextField;
}
- (UIButton *)loginBtn
{
if (!_loginBtn) {
CGFloat pointY = CGRectGetMaxY(_passwordTextField.frame) + 50;
_loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_loginBtn.frame = CGRectMake(kMargin, pointY, kTextFieldWidth, 44);
[_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
_loginBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
[_loginBtn setBackgroundImage:[UIImage imageNamed:@"beijing"] forState:UIControlStateNormal];
[_loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
_loginBtn.layer.cornerRadius = 4.0f;
_loginBtn.layer.masksToBounds = YES;
_loginBtn.enabled = NO;
}
return _loginBtn;
}
#pragma mark -
#pragma mark loginAction
- (void)loginAction:(UIButton *)btn
{
NSLog(@"userName:%@ password:%@", _userNameTextField.text, _passwordTextField.text);
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([_passwordTextField.text isEqualToString:@""] || [_userNameTextField.text isEqualToString:@""]) {
_loginBtn.enabled = NO;
}else{
_loginBtn.enabled = YES;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.view endEditing:YES];
return YES;
}
#pragma mark -
#pragma mark lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithHexString:@"#f7f8f3"];
[self.view addSubview:self.userNameTextField];
[self.view addSubview:self.passwordTextField];
[self.view addSubview:self.loginBtn];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
/***************************************************************/
标签:
原文地址:http://www.cnblogs.com/MrWuYindi/p/5149951.html