iOS UI控件之----UIButton

 1 typedef NS_ENUM(NSInteger, UIButtonType) {
 2     UIButtonTypeCustom = 0,                         // 用户自定义类型
 3     UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // 系统按钮 iOS7以后圆角按钮被 UIButtonTypeSystem 取代 两者等同
 4 
 5     UIButtonTypeDetailDisclosure,            //蓝色箭头按钮
 6     UIButtonTypeInfoLight,                // 亮色感叹号按钮
 7     UIButtonTypeInfoDark,                // 深色感叹号按钮 
 8     UIButtonTypeContactAdd,               //加号按钮
 9     
10     UIButtonTypeRoundedRect = UIButtonTypeSystem,   // 系统按钮 iOS7以后圆角按钮被 UIButtonTypeSystem 取代 两者等同

11 };
//区别并不大,实际开发中一般都是自定义button
复制代码

 

1 //button 创建
2 
3 UIButton * btn = [[UIButton alloc] init];
4 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
复制代码
 1 //button状态
 2 typedef NS_OPTIONS(NSUInteger, UIControlState) {
 3     UIControlStateNormal       = 0,       // 正常状态
 4     UIControlStateHighlighted  = 1 << 0,  // 高亮状态
 5     UIControlStateDisabled     = 1 << 1,  // 失效状态
 6     UIControlStateSelected     = 1 << 2,  // 选中状态
 7     UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3,  // 聚焦状态 (iOS新加入 应该和3D Touch有关)
 8     UIControlStateApplication  = 0x00FF0000,    // 当用做应用标志时
 9     UIControlStateReserved     = 0xFF000000     // 框架预留 无意义
10 };
复制代码
复制代码
 1 //Attributes
 2 
 3 //位置大小
 4 btn.frame = CGRectMake(20, 20, 70, 30);
 5 
 6 //背景色
 7 btn.backgroundColor = [UIColor redColor];
 8 
 9 //标题
10 [btn setTitle:@"按钮" forState:UIControlStateNormal]; // 标题
11 [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; // 标题颜色
12 [btn setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal]; // 标题阴影颜色
13 
14 //image
15 [btn setImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal]; // 不会被拉伸,原比例显示
16 [btn setBackgroundImage:[UIImage imageNamed:@"imageName"] forState:UIControlStateNormal]; // 会被拉伸,充满整个btn
17 
18 //间距
19 btn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);  // btn整体内容四周的间距
20 btn.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); // 标题四周间距
21 btn.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); // 图片四周间距
22 
23 //设置高亮状态按钮是否发光
24 btn.showsTouchWhenHighlighted = YES; // 默认为NO
复制代码
复制代码
//事件
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
//事件状态
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
    UIControlEventTouchDown         = 1 <<  0, // 按下
    UIControlEventTouchDownRepeat   = 1 <<  1, // 多次按下
    UIControlEventTouchDragInside   = 1 <<  2, // 保持按下,在按钮及其一定的外围拖动
    UIControlEventTouchDragOutside  = 1 <<  3, // 保持按下,在按钮外面拖动
    UIControlEventTouchDragEnter    = 1 <<  4, // DragOutside进入DragInside触发
    UIControlEventTouchDragExit     = 1 <<  5, // DragInside到DragOutside触发
    UIControlEventTouchUpInside     = 1 <<  6, // 按钮及其一定外围内松开
    UIControlEventTouchUpOutside    = 1 <<  7, // 按钮外面松开
    UIControlEventTouchCancel       = 1 <<  8, // 点击取消

    ... ...
}
复制代码
复制代码
 1 //获取button相关内容的属性
 2 @property(nullable, nonatomic,readonly,strong) NSString *currentTitle;             // 当前标题
 3 @property(nonatomic,readonly,strong) UIColor  *currentTitleColor;                  // 当前标题颜色
 4 @property(nullable, nonatomic,readonly,strong) UIColor  *currentTitleShadowColor;  // 当前标题阴影颜色
 5 @property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;             // 当前图片
 6 @property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage;   // 当前背景图片
 7 @property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0);  // 当前富文本标题
 8 
 9 @property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel NS_AVAILABLE_IOS(3_0); // 当前标题UILabel
10 @property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0); // 当前图片UIImageView
复制代码
复制代码
1 //绘制
2 
3 - (CGRect)backgroundRectForBounds:(CGRect)bounds;      // 返回背景绘制区域
4 - (CGRect)contentRectForBounds:(CGRect)bounds;         // 返回内容绘制区域
5 - (CGRect)titleRectForContentRect:(CGRect)contentRect; // 返回标题绘制区域
6 - (CGRect)imageRectForContentRect:(CGRect)contentRect; // 返回图片绘制区域
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值