Day 03 of UI
LTView(自定义视图)AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 设置window
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self.window release];
// 创建一个LTView对象,添加到Window上
LTView *useNameLTView = [[LTView alloc] initWithFrame:CGRectMake(50, 50, CGRectGetWidth([UIScreen mainScreen].bounds) - 100, 35)];
useNameLTView.backgroundColor = [UIColor cyanColor];
[self.window addSubview:useNameLTView];
useNameLTView.title = @"用户名";
useNameLTView.placeHolder = @"请输入用户名";
[useNameLTView release];
LTView *passwordLTView = [[LTView alloc] initWithFrame:CGRectMake(50, 150, CGRectGetWidth([UIScreen mainScreen].bounds) - 100, 35) title:@"密码" placeHolder:@"请输入密码"];
passwordLTView.backgroundColor = [UIColor cyanColor];
passwordLTView.isSecureText = 1;
passwordLTView.delegate = self;
[self.window addSubview:passwordLTView];
[passwordLTView release];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"22");
[textField resignFirstResponder];
return YES;
}
LTView.h
@interface LTView : UIView
#pragma mark - 声明属性
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *placeHolder;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, assign) id <UITextFieldDelegate>delegate;
@property (nonatomic, assign) BOOL isSecureText;
#pragma mark - 声明自定义初始化方法
- (instancetype)initWithFrame:(CGRect)frame
title:(NSString *)title
placeHolder:(NSString *)placeHolder;@end
LTView.m
@interface LTView()
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UITextField *textfield;
@end
@implementation LTView
@synthesize text = _text;
#pragma mark - 重写dealloc方法
- (void)dealloc
{
[_label release];
[_textfield release];
[super dealloc];
}
#pragma mark - 重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 1. 创建左侧的Label
self.label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame) / 3, CGRectGetHeight(frame))] autorelease];
self.label.backgroundColor = [UIColor redColor];
[self addSubview:_label];
// 2. 创建右侧的TextField
self.textfield = [[[UITextField alloc] initWithFrame:CGRectMake(CGRectGetWidth(frame) / 3, 0, CGRectGetWidth(frame) * 2 / 3, CGRectGetHeight(frame))] autorelease];
[self addSubview:_textfield];
}
return self;
}
#pragma mark - 重写title的set方法,把外界的值传过来
- (void)setTitle:(NSString *)title
{
if (_title != title) {
[_title release];
_title = [title copy];
_label.text = title;
}
}
- (void)setPlaceHolder:(NSString *)placeHolder
{
if (_placeHolder != placeHolder) {
[_placeHolder release];
_placeHolder = [placeHolder copy];
_textfield.placeholder = _placeHolder;
}
}
#pragma mark - 实现自定义初始化方法
- (instancetype)initWithFrame:(CGRect)frame
title:(NSString *)title
placeHolder:(NSString *)placeHolder
{
self = [self initWithFrame:frame];
if (self) {
_label.text = title;
_textfield.placeholder = placeHolder;
}
return self;
}
-(void)setDelegate:(id<UITextFieldDelegate>)delegate
{
_textfield.delegate = delegate;
}
- (void)setText:(NSString *)text
{
if (_text != text) {
[_text release];
_text = [text copy];
_textfield.text = text;
}
}
- (NSString *)text
{
return _textfield.text;
}
- (void)setIsSecureText:(BOOL)isSecureText
{
_textfield.secureTextEntry = isSecureText;
}
@end