iOS学习之部分常用UIControl控件的属性总结
2016-05-03 本文已影响495人
f94cfadf8756
本文主要介绍UILabel(标签), UITextField(文本框), UIButton(按钮)和UIImageView(图片显示控件)这些常用控件的一些基本属性
首先我们来创建好self.window和根视图控制器吧.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIViewController *vc = [[UIViewController alloc]init];
self.window.rootViewController = vc;
UIView *view = [[UIView alloc] initWithFrame:self.window.frame];
view.backgroundColor = [UIColor whiteColor];
[self.window addSubview:view];
UILabel(标签)
//初始化标签控件:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 175, 100)];
//设置背景颜色:
label.backgroundColor = [UIColor grayColor];
//设置label要显示的文字:(默认黑色)
label.text = @"😄I'm so happy to learn UILabel";
//设置文字对齐方式:(左中右)
label.textAlignment = NSTextAlignmentCenter;//这里也可以写'1'
//设置字体颜色:
label.textColor = [UIColor whiteColor];
//设置字体和大小:
label.font = [UIFont fontWithName:@"Zapfino" size:20];
//设置显示的行数:(0表示自动去换行)
label.numberOfLines = 0;
//设置断行模式:
label.lineBreakMode = NSLineBreakByWordWrapping;//按单词断行,or ByChar(按字符断行)
//设置阴影偏移量:
label.shadowOffset = CGSizeMake(2, 1);
//设置阴影颜色:
label.shadowColor = [UIColor yellowColor];
[view addSubview:label];
UITextField(文本框)
//初始化文本框:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
//设置提示文字:
textField.placeholder = @"请输入用户名";//弹出键盘command+shift+k
//设置边框样式:
textField.borderStyle = UITextBorderStyleRoundedRect;
//设置边框的宽度:
textField.layer.borderWidth = 5.0;
//设置边框颜色:
textField.layer.borderColor = [UIColor yellowColor].CGColor;
//设置文本内容:
textField.text = @"这是文本框";
//设置文本颜色:
textField.textColor = [UIColor redColor];//默认黑色
//设置文本对齐方式:
textField.textAlignment = 0;//默认左对齐,右对齐2,居中1 nsTextAlignmentCenter
//设置字体:
textField.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
//是否允许编辑:
textField.enabled = YES;
//开始输入时清空之前的内容:
textField.clearsOnBeginEditing = YES;
//设置密码输入模式(小点儿)
textField.secureTextEntry = YES;
// 设置键盘的样式:
// textField.keyboardType = UIKeyboardTypeNumberPad;
// 设置键盘右下角按钮内容
// textField.returnKeyType = UIReturnKeyNext;
textField.returnKeyType = UIReturnKeySearch;
//清除按钮(x)样式:
textField.clearButtonMode = UITextFieldViewModeAlways;
// textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//设置左视图:
UIView *leftLittleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
leftLittleView.backgroundColor = [UIColor cyanColor];
textField.leftView = leftLittleView;
//左视图显示的模式:
textField.leftViewMode = UITextFieldViewModeAlways;
//设置右视图:
UIView *rightLittleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
rightLittleView.backgroundColor = [UIColor cyanColor];
textField.rightView = rightLittleView;
//设置右视图模式:
textField.rightViewMode = UITextFieldViewModeAlways;
如果需要还可以设置textField的代理人,实现其代理方法.
textField.delegate = self;
UIButton(按钮)
//初始化按钮:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 175, 64);
[button setTitle:@"我是按钮" forState:UIControlStateNormal];
[view addSubview:button];
//设置边框:
button.layer.borderWidth = 2.0;
button.layer.borderColor = [UIColor greenColor].CGColor;
//设置圆角:
button.layer.cornerRadius = 15;
//设置背景图:
// [button setBackgroundImage:[UIImage imageNamed:@"2.png"] forState:normal];
// //获取按钮的背景图:
// UIImage *backImage = [button backgroundImageForState:UIControlStateNormal];
// NSLog(@"%@", backImage);
// //设置前景图:
// [button setImage:[UIImage imageNamed:@"3.jpg"] forState:UIControlStateNormal];
// UIImage *frontImage = [button imageForState:UIControlStateNormal];
// NSLog(@"%@", frontImage);
//设置按钮字体颜色:
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//获取字体颜色:
UIColor *wordColor = [button titleColorForState:UIControlStateNormal];
NSLog(@"%@", wordColor);
//设置阴影:
//旧版方法!慎用(好像是即将要废除了)
[button setTitleShadowOffset:CGSizeMake(2, 1)];
[button setTitleShadowColor:[UIColor yellowColor] forState:UIControlStateNormal];
//为按钮添加点击事件:
[button addTarget:self action:@selector(didClickedDogButton:) forControlEvents:UIControlEventTouchUpInside];
//为按钮移除点击事件:(少用)
[button removeTarget:self action:@selector(didClickedDogButton:) forControlEvents:UIControlEventTouchUpInside];
UIImageView(图片显示控件)
//创建UIImageView对象:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.window.frame];
imageView.backgroundColor = [UIColor orangeColor];
//为imageView设置图片:
// imageView.image = [UIImage imageNamed:@"2.jpg"];
//植物大战僵尸动画:(素材要自己找)
//(1)设置整体背景:
imageView.image = [UIImage imageNamed:@"BackGround"];//png的图知道名字即可,最好使用png格式的图片
//(2)循环创建动画素材(UIImage对象)
NSMutableArray *flowerArray = [NSMutableArray array];
for (NSInteger i = 1; i <= 18; i++) {
//拼接每个图片的名字:
NSString *name = [NSString stringWithFormat:@"flower%ld.tiff", i];
UIImage *image = [UIImage imageNamed:name];
[flowerArray addObject:image];
}
//(3)创建单独的UIImageView控件
UIImageView *flowerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 80, 73, 74)];
[imageView addSubview:flowerImageView];
//(4)将数组赋值给imageView:
flowerImageView.animationImages = flowerArray;
//(5)设置时间间隔(每两张图片之间播放的时间差)
flowerImageView.animationDuration = 1.8f;
//(6)设置一下重复次数:
// flowerImageView.animationRepeatCount = 5;
//(7)开始动画:
[flowerImageView startAnimating];
//花儿动起来:
[UIView animateWithDuration:8 animations:^{
flowerImageView.frame = CGRectMake(200, 400, 73, 74);
}];
//添加到view上
[view addSubview:imageView];
以上的控件都是很常用的, 也很简单, 适合初学者了解, 我也是刚开始自学, 在空闲时间进行一定的整理, 也给大家分享分享, 如果有什么错误, 或者有一些问题, 还希望大家多多包涵, 欢迎大家一起交流, 让我们一起努力, 共同进步.