您的位置:首页 > 产品设计 > UI/UE

UILabel、UIButton

2015-07-21 14:10 369 查看
UILabel、UIButton
一、UIViewController:视图控制器,用来管理视图
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *viewController = [[ViewController alloc]init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];

二、UILabel:文本控件,用来显示文本内容
1、 初始化:
CGFloat sWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
//sWidth 为屏幕的宽
label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, sWidth - 10 * 2, 200)];

2、添加文字:
label.text = @"我是一只小小小小鸟";

3、改变字体大小:
label.font = [UIFont systemFontOfSize:40];
注:
这里将字体的大小改为40

4、根据label宽度自动适应字体的宽度,来达到显示全部内容的效果
label.adjustsFontSizeToFitWidth = YES;
注:
如果等于NO则不会自动适应字体的宽度

5、更改字体颜色:
label.textColor = [UIColor whiteColor];
注:
whiteColor表示的是将字体的颜色改为白色

6、添加红色阴影:
label.shadowColor = [UIColor redColor];

7、改变阴影方向:
label.shadowOffset = CGSizeMake(3, 5);
注:
1)第一个参数负责左右(宽)(负数:左
正数:右)
2)第二个参数负责上下(高)(负数:上
正数:下)

8、设置文本向左、向右、居中的方法:
label.textAlignment = NSTextAlignmentCenter;
注:
NSTextAlignmentCenter表示设置为居中

9、自动换行:
label.numberOfLines = 100;

10、文本框颜色:
label.backgroundColor = [UIColor clearColor];
注:在iOS7.1之前,默认是有边框颜色的(白色),现在默认是透明的

11、添加到View上:
[self.view addSubview:label];

三、UIButton:按钮控件,点击按钮会触发某个事件
1、初始化:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(120, 400, 120, 120);
[button addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
注:
changeTitle: 为触发的方法名

2、添加字体:
[button setTitle:@"点击" forState:UIControlStateNormal];

3、设置点击时的状态:
[button setTitle:@"确定" forState:UIControlStateHighlighted];

4、点击时设置为高亮状态:
button.showsTouchWhenHighlighted = YES;

5、设置字体颜色:
1)普通状态下的字体颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
2)点击为高亮状态时的字体颜色
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];

5、添加背景图:
1、普通状态时的背景图
[button setBackgroundImage:[UIImage imageNamed:@"27.png"] forState:UIControlStateNormal];
2)点击为高亮状态时的背景图
[button setBackgroundImage:[UIImage imageNamed:@“12.png"] forState:UIControlStateHighlighted];

6、按钮是否被选中(默认是NO):
button.selected = NO;

7、设置背景颜色:
button.backgroundColor = [UIColor grayColor];

8、添加到View:
[self.view addSubview:button];

四、图片视图
UIImageView:用来展示图片
UIImageView *imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
imageView.image = [UIImage imageNamed:@"4127.png"];
[self.view addSubview:imageView];

五、View方法:
1、即将显现的时候调用(视图即将被渲染到屏幕的时候)
- (void)viewWillAppear:(BOOL)animated {
// 可以在viewWillAppear
加载数据
}

2、已经显现的时候调用(视图已经被渲染到屏幕的时候)
- (void)viewDidAppear:(BOOL)animated {

}

3、视图即将消失在屏幕上的时候调用
- (void)viewWillDisappear:(BOOL)animated {

}

4、视图已经消失在屏幕上的时候调用
- (void)viewDidDisappear:(BOOL)animated {

}

5、内存不足的时候调用
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: