//
// ViewController.m
// UIButtonDemo
//
// Created by hehe on 15/9/15.
// Copyright (c) 2015年 wang.hehe. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1.初始化2.设定坐标3.添加到父视图中4.设置属性
UIButton *btn = [[UIButton alloc]init];
btn.frame = CGRectMake(100, 200, 100, 50);
[self.view addSubview:btn];
//button有四种状态
//1.正常状态,normal(enable)
//2.高亮状态,highlight
//3.选择状态,select
//4.禁用状态,disable
//设置文字(标题)
//button上每种状态都可以设置文字。可以不设置,默认是
//正常状态下的文字
//设置正常状态下的文字
[btn setTitle:@"1" forState:UIControlStateNormal];
//[btn setTitle:@"高亮" forState:UIControlStateHighlighted];
//设置选择状态下的文字
[btn setTitle:@"选择" forState:UIControlStateSelected];
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
//设置button为选择状态
//btn.selected = YES;
//设置禁用状态下的
[btn setTitle:@"禁用" forState:UIControlStateDisabled];
[btn setTitleColor:[UIColor greenColor] forState:UIControlStateDisabled];
//btn.enabled = NO;
//设置文字的颜色
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//设置button背景颜色
btn.backgroundColor = [UIColor grayColor];
//button target-action机制
[btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];
//创建一个label对象
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(50, 50, 150, 50);
[self.view addSubview:label];
label.textAlignment = 1;
label.font = [UIFont systemFontOfSize:30];
//NSLog(@"%@",[UIFont familyNames]);
//label.font = [UIFont fontWithName:@"Bodoni 72 Oldstyle" size:30];
label.backgroundColor = [UIColor grayColor];
label.adjustsFontSizeToFitWidth = YES;
label.tag = 100;
//再创建一个button
UIButton *btn1 = [[UIButton alloc]init];
btn1.frame = CGRectMake(100, 300, 100, 50);
[self.view addSubview:btn1];
[btn1 setTitle:@"C" forState:UIControlStateNormal];
//设置文字的颜色
[btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//设置button背景颜色
btn1.backgroundColor = [UIColor grayColor];
//button target-action机制
[btn1 addTarget:self action:@selector(offClick:) forControlEvents:UIControlEventTouchUpInside];
//图片有一个类UIImage
UIImage *img1 = [UIImage imageNamed:@"1.png"];
//如果内存比较大,用下边方法来使用
NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"png"];
NSData *data = [NSData dataWithContentsOfFile:path];
//把数据装换为UIImage对象
UIImage *img2 = [UIImage imageWithData:data];
//设置button图片
[btn setImage:img1 forState:UIControlStateNormal];
[btn setImage:img2 forState:UIControlStateHighlighted];
[btn setBackgroundImage:[UIImage imageNamed:@"7.png"] forState:UIControlStateNormal];
//系统button
UIButton *sysbutton = [UIButton buttonWithType:UIButtonTypeInfoLight];
sysbutton.frame = CGRectMake(50, 150, 60, 30);
sysbutton.backgroundColor = [UIColor yellowColor];
[self.view addSubview:sysbutton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -实现button的处理方法
- (void)onClick:(UIButton *)btm
{
//先找到label
UILabel *label = (id)[self.view viewWithTag:100];
label.text = @"hello";
}
#pragma mark -实现button的处理方法
- (void)offClick:(UIButton *)btm
{
//先找到label
UILabel *label = (id)[self.view viewWithTag:100];
label.text =nil;
}
@end