//
// ViewController.m
// UIControl
//
// Created by HarrySun on 16/7/11.
// Copyright © 2016年 Mobby. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 不知道这个怎么写,就拿出来UIButton里面的东西对应着写了些
// UIButton *button = [UIButton buttonWithType:(UIButtonTypeContactAdd)]; // button 遍历构造器
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 100, 50)];
[self.view addSubview:button];
button.backgroundColor = [UIColor redColor]; // 背景颜色
[button setTitle:@"按钮" forState:(UIControlStateNormal)]; // 设置标题,未选中时状态
button.titleLabel.font = [UIFont systemFontOfSize:25]; //设置标题大小
[button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)]; // 设置按钮在某个状态下的标题颜色
[button setTitleColor:[UIColor orangeColor] forState:(UIControlStateHighlighted)];
button.showsTouchWhenHighlighted = YES; // 设置按钮按下会发光
button.adjustsImageWhenHighlighted = YES; // 按下高亮的时候,图像的颜色是否要加深一点
// button.enabled = NO; // 设置控件启用
// button.selected = NO; // 当用户选择控件时,UIControl类会将selected属性设置为YES
// button.highlighted = YES; // 高亮状态
// button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; // 控件如何在垂直方向上布置自身的内容
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; // 控件如何在水平方向上布置自身的内容
[button addTarget:self action:@selector(buttonEvent:) forControlEvents:(UIControlEventTouchUpInside)]; // 添加事件
// [button setImage:[UIImage imageNamed:@"1"] forState:(UIControlStateNormal)]; // 高亮状态下显示图片
button.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10); // 设置按钮的内容内容(包括按钮图片和标题)离按钮边缘上左下右的距离
button.titleEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10); // 设置按钮的内部标题离按钮边缘上左下右的距离
// button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10); // 设置按钮的内部图片离按钮边缘上左下右的距离
NSLog(@"%@",button.currentTitle); // 获取按钮当前标题,只读
NSLog(@"%@",button.currentAttributedTitle); // 获取按钮当前富文本标题,只读
NSLog(@"%@",button.currentTitleColor); // 获取按钮当前标题颜色,只读
NSLog(@"%@",button.currentTitleShadowColor); // 获取按钮当前标题的阴影颜色
NSLog(@"%@",button.currentImage); // 获取按钮当前图片
NSLog(@"%@",button.currentBackgroundImage); // 获取按钮当前北京图片
}
- (void)buttonEvent:(UIButton *)sender{
NSLog(@"button 点击");
// 取消button已经添加的所有事件
[sender removeTarget:nil action:nil forControlEvents:(UIControlEventTouchUpInside)];
NSSet *myActions = [sender allTargets]; // 获取当前控件上所有指定动作
NSLog(@"%@",myActions);
NSArray *myAction = [sender actionsForTarget:self forControlEvent:(UIControlEventValueChanged)]; // 获取某一特定事件目标的全部动作列表
NSLog(@"%@",myAction);
sender.enabled = NO;
sender.adjustsImageWhenDisabled = YES; // 按钮被禁用的时候,图像的颜色是否要加深一些(需要按钮有图片才能看出来)
}
#pragma mark - 重写绘制行为
/*
你可以通过子类化按钮来定制属于你自己的按钮类。在子类化的时候你可以重载下面这些方法,这些方法返回CGRect结构,指明了按钮每一组成部分的边界。
注意:不要直接调用这些方法,这些方法是写给系统调用的
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
- (CGRect)contentRectForBounds:(CGRect)bounds;
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
- (CGRect)imageRectForContentRect:(CGRect)contentRect;
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end