//创建UILabel函数
- (void) creatUILabel{
//定义并创建一个UILabel对象
UILabel* label = [[UILabel alloc] init];
//文字赋值,字符串对象,设定字体大小,颜色,阴影,阴影偏移量,对齐方式,显示行数(默认1,设为0时智能按合适的行数显示)
label.text = @"你好啊,how are you 你好啊,how are you 你好啊,how are you";
label.font = [UIFont systemFontOfSize:12];
label.textColor = [UIColor blueColor];
label.shadowColor = [UIColor grayColor];
label.shadowOffset = CGSizeMake(2, 1);
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 0;
//设定label的显示位置(左坐标,上坐标,宽,高)
label.frame = CGRectMake(100, 400, 160, 40);
//设定label的背景颜色,clearColor透明色
label.backgroundColor = [UIColor grayColor];
//将label显示到屏幕上
//当前.视图 增加子视图:label
[self.view addSubview:label];
}
//创建普通按钮函数
- (void) createUIRectButton{
//创建圆角按钮
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100, 100, 60);
//设置按钮文字内容,正常状态和按下状态,设置文字颜色
[btn setTitle:@"按钮1" forState:UIControlStateNormal];
[btn setTitle:@"按钮被按下" forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//设置背景颜色
btn.backgroundColor = [UIColor grayColor];
//添加事件
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
//添加到视图中显示
[self.view addSubview:btn];
}
//创建显示图片按钮函数
- (void) createImageBtn{
//创建一个自定义类型的btn
UIButton* btnImage = [UIButton buttonWithType:UIButtonTypeSystem];
btnImage.frame = CGRectMake(100, 200, 100, 100);
//显示图片
UIImage* icon01 = [UIImage imageNamed:@"2.png"];
[btnImage setImage:icon01 forState:UIControlStateNormal];
//添加事件
[btnImage addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnImage];
//添加按钮唯一标记
btnImage.tag = 101;
}
//响应按钮按下事件函数
- (void) pressBtn:(UIButton*) btn{
if(btn.tag == 101){
NSLog(@"hahaha...");
}else{
NSLog(@"hehehe...");
btn.backgroundColor = [UIColor redColor];
}
}
//程序入口
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//调用创建UI函数
[self creatUILabel];
[self createUIRectButton];
[self createImageBtn];
}
//按钮显示网络图片
int all_number = (int)[responseObject[@"data"] count];
for (NSInteger i = 0; i < all_number; i++) {
NSURL * imageUrl = [NSURL URLWithString: responseObject[@"data"][i][@"avatar"]];
UIImage * image = [UIImage imageWithData: [NSData dataWithContentsOfURL: imageUrl]];
//设置图片图片为主,不拉伸
//[self.four_site[i] setImage:image forState:normal];
//设置按钮背景图片,按钮为主,图片上的字依然会显示,按钮变大图片会拉伸
[self.four_site[i] setBackgroundImage: image forState: normal];
}
IOS label和button
点赞
收藏