#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.UIButton
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];// 自定义button,现在oc追求扁平化,有些效果
button.frame = CGRectMake(self.view.frame.size.width/2.0-100/2.0, 150, 100, 45) ;
[button setTitle:@"未收藏" forState:UIControlStateNormal];
[button setTitle:@"已收藏" forState:UIControlStateSelected];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
button.backgroundColor = [UIColor brownColor];
// button.highlighted = YES;
// button.enabled = YES;
[button addTarget:self action:@selector(changeImage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
button.tag = 101;
button.showsTouchWhenHighlighted = YES;// 点上去发一下光
//改字体大小
button.titleLabel.font = [UIFont systemFontOfSize:30];
//设置图片
UIImage * image = [UIImage imageNamed:@"010@2x"];
[button setImage:image forState:UIControlStateSelected];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight ;// 右对齐
UIEdgeInsets Imageinset = {0,10,0,0};// 上左下右
[button setImageEdgeInsets:Imageinset];
UIEdgeInsets titleInset = {0,20,20,0};
[button setTitleEdgeInsets:titleInset];
}
- (void)changeImage{
UIButton * btn =(UIButton *)[self.view viewWithTag:101];
btn.selected = !btn.selected;
}