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

button上加上图片的两种方式的更多相关文章

  1. jQuery 实现图片放大两种方式

    jQuery 实现图片放大两种方式 一.利用css样式表实现,多用于后台显示 1.这种比较简单,利用dom元素的hover实现样式切换 <style> img{ cursor: point ...

  2. 转载:删除github上文件夹的两种方式

    http://www.jianshu.com/p/286be61bb9b8 删除github上文件夹的两种方式(解决已经加入ignore的文件夹无法从远程仓库删除的问题) 如果此文件夹已被加入git追 ...

  3. Nodejs 传图片的两种方式

    node上传图片第一种方式 1,首先引入模块 "connect-multiparty": "~1.2.5", 在package.json中添加 "co ...

  4. UIImage加载本地图片的两种方式

    UIImage加载图片方式一般有两种: (1)imagedNamed初始化:默认加载图片成功后会内存中缓存图片,这个方法用一个指定的名字在系统缓存中查找并返回一个图片对象.如果缓存中没有找到相应的图片 ...

  5. UIImage创建图片的两种方式的区别

    在工作中经常会遇到添加图片,用哪种方式添加更好呢?请看详解 方法一: UIImage *image = [UIImage imageNamed:@"haha"]; 这种方法创建的图 ...

  6. android绘制圆形图片的两种方式

    看下效果先 下面有完整的示例代码 使用BitmapShader(着色器) 我们在绘制view 的时候 就是小学上美术课 用水彩笔在本子上画画 使用着色器绘制圆形图片最简单的理解方式 就是把bitmap ...

  7. Laravel 上使用 phpexcel的两种方式

    原创 2017年06月24日 20:24:31 1229 文章采集与网上 方式1.使用原生的phpexcel , http://blog.csdn.net/CSwfe/article/details/ ...

  8. Github 上传代码的两种方式

    上传本地代码/文件->Github 折腾了半天时间... Github前期准备部分 1)登录github,新建一个 repository 2)repository 命名 3)Github是一个托 ...

  9. Django上传文件的两种方式

    基于form表单上传文件 HTML <h3>基于form表单的上传文件</h3> <form action="" method="post& ...

随机推荐

  1. linux下的shell命令的编写,以及java怎样调用linux的shell命令(java怎样获取linux上的网卡的ip信息)

    程序猿都非常懒,你懂的! 近期在开发中,须要用到server的ip和mac信息.可是server是架设在linux系统上的,对于多网口,在获取ip时就产生了非常大的问题.以下是在windows系统上, ...

  2. HDU 4121 Xiangqi 模拟题

    Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...

  3. Codeforces Gym 100286A. Aerodynamics 计算几何 求二维凸包面积

    Problem A. AerodynamicsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...

  4. ORACLE 11G用于有效期

    Oracle报错,ORA-28001: 口令已经失效(转自网络) 错误信息:ORA-28001: the password has expired解决方法 Oracle11G创建用户时缺省passwo ...

  5. 彩票APP将演绎“快鱼吃慢鱼”的发展轨迹

    思科CEO钱伯斯有句名言,叫"快鱼吃慢鱼",他觉得"在Internet经济下,大公司不一定打败小公司,可是快的一定会打败慢的."对此观点,笔者表示部分认同,首先 ...

  6. 查看修改swap空间大小

    1  查看swap 空间大小(总计):      # free -m          默认单位为k, -m 单位为M                total       used       fr ...

  7. Panopticon跨平台的逆向工程反汇编工具

    http://www.freebuf.com/sectool/104045.html Panopticon 使用GPLv3授权许可,其免费. 项目文档:https://panopticon.re. 问 ...

  8. C语言中堆和栈的区别

    原文:http://blog.csdn.net/tigerjibo/article/details/7423728 C语言中堆和栈的区别 一.前言: C语言程序经过编译连接后形成编译.连接后形成的二进 ...

  9. mysql权限及用户

    一:Flush table tables_name MySQL的FLUSH句法(清除或者重新加载内部缓存) FLUSH flush_option [,flush_option],如果你想要清除一些My ...

  10. Linux shell 脚本攻略之正则表达式入门

    摘自:<Linux shell 脚本攻略> 下面是类似的解释: