#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate> {
UILabel *label_;
UIButton *button_;
UITextField *textField_;
}
@end
@implementation ViewController
- (void)loadView {
// 为了点击背景关闭键盘,需要让背景支持处理触摸事件
// 因此视图控制器的视图必须从UIControl或者其派生类构建
// 当背景视图TouchDown时,调用resignKeyboard:方法来关闭键盘
UIControl *view = [[UIControl alloc] init];
[view addTarget:self action:@selector(resignKeyboard:) forControlEvents:UIControlEventTouchDown];
self.view = view;
[view release];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.label];
[self.view addSubview:self.button];
[self.view addSubview:self.textField];
// 添加观察者监听键盘弹出和收回的通知begin开始 editing编辑
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UITextFieldTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UITextFieldTextDidEndEditingNotification object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// 移除观察者
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark getter
- (UILabel *)label {
// 创建一个UILabel alignment对齐 system系统 break间断 middle中间 truncating截断
//shadow阴影
if (!label_) {
label_ = [[[UILabel alloc] initWithFrame:CGRectMake(20, 100, 250, 60)] autorelease];
label_.text = @"今天天气不错我要去公园玩但是我大了爸爸不同意";
label_.textAlignment = NSTextAlignmentCenter;
label_.textColor = [UIColor redColor];
label_.font = [UIFont systemFontOfSize:18];
label_.lineBreakMode = NSLineBreakByTruncatingMiddle;
// label_.shadowColor = [UIColor darkGrayColor];
// label_.shadowOffset = CGSizeMake(10, 10);
label_.backgroundColor = [UIColor yellowColor];
}
return label_;
}
- (UIButton *)button {
// UIButton state状态 normal正常 disabled残缺的(在这里指取消)
//highlighted加亮,突出 enabled启用
if (!button_) {
button_ = [UIButton buttonWithType:UIButtonTypeSystem];
button_.frame = CGRectMake(20, 200, 80, 30);
[button_ setTitle:@"确定" forState:UIControlStateNormal];
[button_ setTitle:@"取消" forState:UIControlStateDisabled];
[button_ setTitle:@"好亮" forState:UIControlStateHighlighted];
[button_ setTitle:@"被选" forState:UIControlStateSelected];
button_.backgroundColor = [UIColor yellowColor];
// button_.enabled = NO;
// button_.highlighted = YES;
// button_.selected = YES;
[button_ addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return button_;
}
//border边界 style风格、设计 clear清除 always永远 placeholder占位文本
//search搜索
- (UITextField *)textField {
if (!textField_) {
textField_ = [[[UITextField alloc] initWithFrame:CGRectMake(20, 250, 200, 30)] autorelease];
textField_.borderStyle = UITextBorderStyleLine;
textField_.clearButtonMode = UITextFieldViewModeAlways;
textField_.backgroundColor = [UIColor yellowColor];
textField_.placeholder = @"请输入IQ值";
textField_.text = @"123";
textField_.returnKeyType = UIReturnKeySearch;
// textField_.keyboardType = UIKeyboardTypeNumberPad;
//accessory附件
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 48)];
inputView.backgroundColor = [UIColor greenColor];
// textField_.inputView = inputView;
textField_.inputAccessoryView = inputView;
[inputView release];
textField_.delegate = self;
}
return textField_;
}
//pragma 编译指示 should将要,应该 responder响应器
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
self.label.text = textField_.text;
[textField_ resignFirstResponder];
return YES;
}
#pragma mark Actions
- (IBAction)resignKeyboard:(id)sender {
[self.textField resignFirstResponder];
}
//show显示
- (void)keyboardShow:(NSNotification *)notify {
NSLog(@"出来了");
NSLog(@"%@", notify.userInfo);
}
//hide隐藏
- (void)keyboardHide:(NSNotification *)notify {
NSLog(@"回去了");
NSLog(@"%@", notify.userInfo);
}
- (IBAction)buttonAction:(id)sender {
// [self.textField resignFirstResponder];
[self.textField endEditing:YES];//是否隐藏键盘
self.label.text = self.textField.text;
}
@end