button上加上图片的两种方式
//
// 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上加上图片的两种方式的更多相关文章
- jQuery 实现图片放大两种方式
jQuery 实现图片放大两种方式 一.利用css样式表实现,多用于后台显示 1.这种比较简单,利用dom元素的hover实现样式切换 <style> img{ cursor: point ...
- 转载:删除github上文件夹的两种方式
http://www.jianshu.com/p/286be61bb9b8 删除github上文件夹的两种方式(解决已经加入ignore的文件夹无法从远程仓库删除的问题) 如果此文件夹已被加入git追 ...
- Nodejs 传图片的两种方式
node上传图片第一种方式 1,首先引入模块 "connect-multiparty": "~1.2.5", 在package.json中添加 "co ...
- UIImage加载本地图片的两种方式
UIImage加载图片方式一般有两种: (1)imagedNamed初始化:默认加载图片成功后会内存中缓存图片,这个方法用一个指定的名字在系统缓存中查找并返回一个图片对象.如果缓存中没有找到相应的图片 ...
- UIImage创建图片的两种方式的区别
在工作中经常会遇到添加图片,用哪种方式添加更好呢?请看详解 方法一: UIImage *image = [UIImage imageNamed:@"haha"]; 这种方法创建的图 ...
- android绘制圆形图片的两种方式
看下效果先 下面有完整的示例代码 使用BitmapShader(着色器) 我们在绘制view 的时候 就是小学上美术课 用水彩笔在本子上画画 使用着色器绘制圆形图片最简单的理解方式 就是把bitmap ...
- Laravel 上使用 phpexcel的两种方式
原创 2017年06月24日 20:24:31 1229 文章采集与网上 方式1.使用原生的phpexcel , http://blog.csdn.net/CSwfe/article/details/ ...
- Github 上传代码的两种方式
上传本地代码/文件->Github 折腾了半天时间... Github前期准备部分 1)登录github,新建一个 repository 2)repository 命名 3)Github是一个托 ...
- Django上传文件的两种方式
基于form表单上传文件 HTML <h3>基于form表单的上传文件</h3> <form action="" method="post& ...
随机推荐
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- c# 自己制作一个简单的项目倒计时器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- CCBReader
#ifndef _CCB_CCBREADER_H_ #define _CCB_CCBREADER_H_ #include "cocos2d.h" #include "Ex ...
- 【Android开发学习之路】
http://blog.csdn.net/wwj_748/article/category/1119319
- android拦截短信并屏蔽系统的Notification
拦截短信有几个关键点: 1.android接收短信时是以广播的方式 2.程序只要在自己的Manifest.xml里加有"接收"SMS的权限 <uses-permission ...
- Android Bundle传递简单数据、对象数据
Android开发过程中进程遇到组件之间.进程之间等数据的传递,数据传递有非常多种,当中使用Bundle传递非常方便. Bundle能够传递多种数据,是一种类似map的key-value数据结构 简单 ...
- ABAP FIELD-SYMBOLS 有大作用- 将没有可改参数的增强出口变得也能改主程序的值了
看下图代码: report z_xul_test2 中 定义了 全局变量 G_DATA1 , 分别调用了 z_xul_tes1 中的 form 和 function zbapi_test , 这两 ...
- Anaconda packages list
# packages in environment at D:\Applications\Anaconda3:#alabaster 0.7.6 py35_0 anaconda 2.4.0 np110p ...
- 进程环境之getrlimit和setrlimit函数
每个进程都有一组资源限制,其中一些可以用getrlimit和setrlimit函数查询和更改. #include <sys/resource.h> int getrlimit( int r ...
- 高级I/O之记录锁
若两个人同时编辑一个文件,其后果将如何呢?在很多UNIX系统中,该文件的最后状态取决于写该文件的最后一个进程.但是对于有些应用程序(例如数据库),进程有时需要确保它正在单独写一个文件.为了向进程提供这 ...