// ViewController.m
#import "ViewController.h"
@interfaceViewController ()
@end
@implementationViewController
// UITextField(输入框):是控制文本输入和显示的控件。在App中UITextField出现频率也⽐比较高。iOS系统借助虚拟键盘实现输入,当点击输入框,系统会⾃动调出键盘,⽅便你进⼀步操作。在你不需要输入的时候,可以使用收回键盘的方法,收回弹出的键盘。UITextField和UILabel相比,UILabel主要用于⽂字显示,不能编辑,UITextField允许⽤户编辑⽂字(输⼊入)。
//UIButton(按钮):是响应⽤户点击的控件。在App中UIButton是出现频率很高的控件。UIButton与UILabel、UITextField侧重点不同,侧重于处理点按。当然UIButton类也提供了⼀一些⽅法控制按钮外观。
- (void)viewDidLoad {
[superviewDidLoad];
// // 根据图片名获取图片
// UIImage *image = [UIImageimageNamed:@"QQ"];
// // 自定义图片视图
// UIImageView *imageView = [[UIImageViewalloc]initWithImage:image];
//
// // 创建一个UItextField对象
// // UItextField继承于UIView,侧重文本输入
// UITextField *textField =[[UITextFieldalloc]initWithFrame:CGRectMake(100, 100, 200, 30)];
// // 设置背景颜色
// textField.backgroundColor = [UIColororangeColor];
// textField.tag = 100;
// // 文本输入控制
// //设定一个初始化文本
// // textField.text = @"请输入";
// // 设置占位符
// textField.placeholder = @"请输入";
// //字体对齐方式
// textField.textAlignment = NSTextAlignmentCenter;
// //字体颜色
// textField.textColor = [UIColorblackColor];
// // 字体大小,weight是指字体的粗细程度
// textField.font = [UIFont systemFontOfSize:20 weight:0.1];
// // 密文输入
// textField.secureTextEntry = YES;
// // 清除文本按钮
// textField.clearButtonMode = UITextFieldViewModeWhileEditing;
// //将此textField变成第一响应者
// [textFieldbecomeFirstResponder]; //即输入框出现光标
// // 将textField取消第一响应者
[textFieldresignFirstResponder];
// // 键盘模式
// textField.keyboardType = UIKeyboardTypeURL;
// //设置边粗细
// textField.layer.borderWidth = 1;
// //设置边框颜色
// textField.layer.borderColor = [UIColorblackColor].CGColor;
// //设置圆角
// textField.layer.cornerRadius = 10;
//
// // 辅助视图
// // 辅助视图的frame在系统方法内部重新设置了
// UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(100, 100, 20,20)];
// label.backgroundColor = [UIColorredColor];
// // textFieldAccount.inputAccessoryView = label;
// // 输入视图,可以设置自定义键盘
textFieldAccount.inputView = label;
//
// // 键盘返回模式
// textField.returnKeyType = UIReturnKeySearch;
//
// // 添加到视图上
// [self.viewaddSubview:textField];
//
// // 外观配置
// // 设置圆框
// textField.borderStyle = UITextBorderStyleRoundedRect;
// // 设置左视图
// textField.leftView = imageView;
// // 左视图显示模式
// textField.leftViewMode = UITextFieldViewModeAlways;
//
// // 设置textField的代理
// // self 是当前类的对象
// textField.delegate = self;
//
// UITextField *textFieldCancel = [[UITextFieldalloc]initWithFrame:CGRectMake(200,200, 100, 30)];
// textFieldCancel.backgroundColor = [UIColorredColor];
// textFieldCancel.tag = 101;
// textFieldCancel.delegate = self;
// [self.viewaddSubview:textFieldCancel];
//
// [textField release];
// [textFieldCancel release];
// [label release];
// [imageView release];
// UIButton:侧重于点击,对应方法
// 由UILabel和UIImageView组成
// 创建一个button对象
UIButton *but =[UIButtonbuttonWithType:UIButtonTypeCustom];// 要用custom 才能用前景图片
// 设置属性
but.frame = CGRectMake(100, 100, 200, 80);
but.backgroundColor = [UIColororangeColor];
// 设置but的标题
[but setTitle:@"戳我" forState:UIControlStateNormal];
[but setTitle:@"戳它" forState:UIControlStateHighlighted];
// 设置标题颜色
[butsetTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];
// 设置标题大小
but.titleLabel.font = [UIFontsystemFontOfSize:30];
//点击高亮
but.showsTouchWhenHighlighted = YES;
// 设置背景图片
// [butsetBackgroundImage:[UIImageimageNamed:@"QQ.png"] forState:UIControlStateNormal];
// 设置前景图片
[butsetImage:[UIImageimageNamed:@"QQ"]forState:UIControlStateNormal];
// 设置图片距离上左下右的间隙
[butsetImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 100)];
// 这里的target决定了方法由谁执行,一般来说给self。
[butaddTarget:self action:@selector(tap:)forControlEvents:UIControlEventTouchUpInside];
// 添加到视图
[self.viewaddSubview:but];
[but release];
}
//button的点击事件
- (void)tap:(UIButton *)but{
NSLog(@"呵呵,又被戳了");
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)buttonAciton:(UIButton *)button{
NSLog(@"登陆成功");
}
//输入框能否开始编辑
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
// return NO to disallow editing.
return YES;
}
// 开始编辑
-(void)textFieldDidBeginEditing:(UITextField *)textField{
// became first responder
NSLog(@"start editing");
}
//能否结束编辑,(设置NO,点击另一个输入框没有反应)
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
// return YES to allow editing to stop and to resign first responderstatus. NO to disallow the editing session to end
// 当输入字符串的长度大于 5 时,才可以结束编辑
if(textField.text.length> 5){
return YES;
}else{
return NO;
}
}
// 输入框已经结束编辑,转换到另一个输入框时执行。
- (void)textFieldDidEndEditing:(UITextField*)textField{
// may be called if forced even if shouldEndEditing returns NO (e.g.view removed from window) or endEditing:YES called
NSLog(@"结束了");
}
- (BOOL)textField:(UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString *)string{
//range.location说的是光标的位置
// string 是一个一个的字节
NSLog(@"%@",string);
NSLog(@"%ld",range.location);
NSLog(@"%@",textField.text);
if (textField.text.length> 10) {
return NO;
}else{
return YES;
}
}
// 文本框清除按钮能否清除
- (BOOL)textFieldShouldClear:(UITextField*)textField{
// called when clear button pressed. return NO to ignore (nonotifications)
return YES;
}
// 一般我们在这个方法对键盘进行回收
- (BOOL)textFieldShouldReturn:(UITextField*)textField{
// called when 'return' key pressed. return NO to ignore.
// [textFieldresignFirstResponder];
UITextField *tf = (UITextField *)[self.viewviewWithTag:101];
if (textField.tag == 100) {
[textFieldresignFirstResponder];
[tfbecomeFirstResponder];
}
if (textField.tag == 101) {
[tfresignFirstResponder];
}
return YES;
}
// 获取视图的几种方式
// 1、tag值
// 2、属性
// 3、根据父子视图的层级关系
@end