//
//  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. 创建MySQL从库

    我们知道Oracle有DataGuard实时备份数据.能够做主备切换,而MySQL也有自己的一套备库方案.称之为主从复制. 搭建MySQL从库是为了实时同步主库数据,同一时候也能够分担主库的读压力.对 ...

  2. some websit

    Baidu:VideoView onVideoSizeChanged http://code.taobao.org/p/TangHuZhao/src/ http://code.taobao.org/p ...

  3. [AngularJS] Using ngModel in Custom Directives

    You can use ngModel in your own directives, but there are a few things you'll need to do to get it w ...

  4. ios开发——面试篇C语言精华

    面试篇C语言精华    1.面向过程:分析解决问题所需要的步骤,然后用函数把这些步骤一步一步实 现. 面向对象:直接描述客观世界的对象及其相互关系.现实世界中任何实体都 可以看作是对象,对象之间通过消 ...

  5. Android下将图片载入到内存中

    Android的系统的标准默认每一个应用程序分配的内存是16M.所以来说是很宝贵的,在创建应用的时候要尽可能的去节省内存,可是在载入一些大的文件的时候,比方图片是相当耗内存的,一个1.3M的图片,分辨 ...

  6. gdb在运行maintenance info program-spaces命令时coredump

    coredump时的信息: (gdb) maintenance info program-spaces *** Error in `gdb': free(): invalid pointer: 0x0 ...

  7. Java多线程中start()和run()的区别

    Java的线程是通过java.lang.Thread类来实现的.VM启动时会有一个由主方法所定义的线程.可以通过创建Thread的实例来创建新的线程.每个线程都是通过某个特定Thread对象所对应的方 ...

  8. Python学习 之 OS模块

    1.目录操作 import os os.mkdir('abc')   #创建abc文件 tree a  #查看目录结构 2.目录遍历 方式一:递归 import os def dirList(path ...

  9. Python学习 之 数据类型(邹琪鲜 milo)

    1.Python中的数据类型:数字.字符串.列表.元组.字典 2.数字类型包括整型.长整型.浮点型.复数型 type(number):获取number的数据类型 整型(int):范围:-2,147,4 ...

  10. 如何用eclispe远程调试tomcat--转载

    原文地址:http://jingyan.baidu.com/article/0320e2c1f4ef6b1b87507b06.html tomcat是一种非常常见的java web应用服务器,有时候服 ...