UIButton

//
//  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];
}

- (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

转载于:https://www.cnblogs.com/wanghengheng/p/4809710.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值