1.UIButton在代码中的使用及监听点击事件
基本使用:
//创建一个按钮
//注意:设置按钮类型只能在初始化的时候设置,因为类型为只读类型
UIButton *button = [[UIButton alloc] init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //设置按钮的类型
//设置frame
button.frame = CGRectMake(0,0,100,70);
//设置背景颜色
button.backgroundColor = [UIColor greenColor];
[button setBackgroundColor:[UIColor greenColor]];
//设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"a"] forState:UIControlStateNormal];
//设置图片和文字(普通状态下)
[button setImage:[UIImage imageNamed:@"jjj"] forState:UIControlStateNormal];
[button setTitle:@"文字部分" forState:UIControlStateNormal];
//设置图片和文字(高亮状态下)
[button setImage:[UIImage imageNamed:@"jjj"] forState:UIControlStateHignlighted];
[button setTitle:@"文字部分" forState:UIControlStateHignlighted];
//设置文字颜色
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
//设置文字的阴影颜色
[button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
//设置阴影的偏移量,需要和阴影颜色结合使用
button.titleLabel.shadowOffset = CGSizeMake(3,2);
//加到当前控制器的view中
[self.view addSubview:button];
监听点击事件:
方式1:连线
-(IBAction)clickButton:(UIButton *)button{
//代码...
}
方式2:纯代码
/*
Target:目标,让谁做事情
action:方法,做什么事情,当方法有参数时,加一个冒号:
events:事件
*/
[button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
2.UIButton、UILabel、UIImage的选择
UIButton:
特点:既能显示文字,也能显示图片
长按高亮的时候可以切换图片或文字
直接通过addTarget…方法监听点击事件
ImageView:
特点:只能显示图片
不能通过addTarget…方法监听点击事件
label:
特点:只能显示文字
不能通过addTarget…方法监听点击事件
如果是仅仅显示数据,不需要点击:建议选择UIImageView UILabel
如果不仅显示数据,还需要监听点击事件:建议选择UIButton
长按控件可以改变显示的内容:UIButton
同时显示两张图片,既要显示内容图片又要显示背景图片:UIButton
3.为UIButton调整内部子空间的位置
方法1:重写方法
//新建一个类继承自UIButton
//MyUIButton.m
#import "MYUIButton.m"
@implementation
-(instancetype)initWithFrame:(CGRect)frame{
if(self=[super initWithFrame:frame]){
//文本居中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.imageView.contentMode = UIViewContentModeCenter;
}
return self;
}
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
return CGRectMake(0,0,100,70);
}
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
return CGRectMake(0,0,100,70);
}
@end
方式2:
//MyUIButton.m
#import "MYUIButton.m"
@implementation
//再执行该方法
-(instancetype)initWithFrame:(CGRect)frame{
if(self=[super initWithFrame:frame]){
//文本居中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.imageView.contentMode = UIViewContentModeCenter;
}
return self;
}
//先执行该方法
-(void)layoutSubview{
self.titleLabel.frame = CGRectMake(0,0,100,70);
self.imageView.frame = CGRectMake(100,0,70,70);
}
@end
实现
//ViewController.m
#import "ViewController.h"
#import "MYUIButton.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)viewDidLoad{
[super viewDidLoad];
//1.1创建按钮
MYUIButton *button = [MYUIButton buttonWithType:UIButtonTypeCustom];
//1.2设置frame
button.frame = CGRectMake(100,100,170,70);
//1.3设置背景颜色
button.backgroundColor = [UIColor greenColor];
//1.4设置内容图片
[button setImage:[UIImage imageNamed:@"123"] forState:UIControlStateNormal];
//1.5设置文字内容
[button setTitle:@"title" forState:UIControlStateNormal];
[self.view addSubview:button];
}
@end
4.UIButton的内边距设置
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,170,70);
[button setImage:[UIImage imageNamed:@"123"] forState:UIControlStateNormal];
[button setTitle:@"titleText" forState:UIControlStateNormal];
button.contentEdgeInsets = UIEdgeInsetsMake(10,0,0,0);
button.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,10);
button.titleEdgeInsets = UIEdgeInsetsMake(0,0,0,-10);
5.背景图片的拉伸
为了拉伸图片且图片不变形
方式1:
//思路:只拉伸中间1像素
UIImage *image = [UIImage imageNamed:@"123"];
//返回一张受保护而且拉伸的图片
UIImage *resizableImage = [image resizableImageWithCapInsets: UIEdgeInsetsMake(image.frame.size.height/2, image.frame.size.width/2, image.frame.size.height/2-1, image.frame.size.width/2-1)];
//resizingMode
//UIImageResizingModeTile:平铺
//UIImageResizingModeStretch:拉伸
UIImage *resizableImage = [image resizableImageWithCapInsets: UIEdgeInsetsMake(image.frame.size.height/2, image.frame.size.width/2, image.frame.size.height/2-1, image.frame.size.width/2-1) resizingMode: UIImageResizingModeTile];
//把图片设置到按钮上
[self.button setBackgroundImage:image forState:UIControlStateNormal];
方式2:
//右边需要保护的区域 = 图片的宽度 - leftCapWidth - 1
//底部需要保护的区域 = 图片的高度 - topCapHeight - 1
[image stretchableImageWithLeftCapWidth:image.frame.size.width * 0.5 topCapHeight:image.frame.size.height * 0.5];