iOS控件之UIButton

创建

UIButton * btn = [[UIButton alloc] init];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
typedef NS_ENUM(NSInteger, UIButtonType) {
    UIButtonTypeCustom = 0,                         // 自定义按钮
    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // 系统按钮 iOS7以后圆角按钮被 UIButtonTypeSystem 取代 两者等同
    
    UIButtonTypeDetailDisclosure,                   // 蓝色箭头按钮(用作详
    UIButtonTypeInfoLight,                          // 亮色感叹号按钮
    UIButtonTypeInfoDark,                           // 深色感叹号按钮
    UIButtonTypeContactAdd,                         // 加号按钮
    
    UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead
};

状态

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,       // 正常状态
    UIControlStateHighlighted  = 1 << 0,  // 高亮状态
    UIControlStateDisabled     = 1 << 1,  // 失效状态
    UIControlStateSelected     = 1 << 2,  // 选中状态
    UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3,  // 聚焦状态 (iOS新加入 应该和3D Touch有关)
    UIControlStateApplication  = 0x00FF0000,    // 当用做应用标志时
    UIControlStateReserved     = 0xFF000000     // 框架预留 无意义
};

属性

// frame

btn.frame = CGRectMake(20, 20, 70, 30);

// 背景色

btn.backgroundColor = [UIColor redColor];

// 标题

[btn setTitle:@"按钮" forState:UIControlStateNormal]; // 标题
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // 标题颜色
[btn setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal]; // 标题阴影颜色

// 图片

[btn setImage:[UIImage imageNamed:@"btnIcon"] forState:UIControlStateNormal]; // 不会被拉伸,原比例显示
[btn setBackgroundImage:[UIImage imageNamed:@"btnBgImg"] forState:UIControlStateNormal]; // 会被拉伸,充满整个btn

// 间距

btn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);  // btn整体内容四周的间距
btn.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); // 标题四周间距
btn.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); // 图片四周间距

// 设置高亮状态 加深图片颜色

btn.adjustsImageWhenHighlighted = NO; // 默认为YES 设置为NO取消效果

// 设置失效状态 加深图片颜色

btn.adjustsImageWhenDisabled = NO; // 默认为YES 设置NO取消效果

// 设置高亮状态 按钮发光

btn.showsTouchWhenHighlighted = YES; // 默认为NO 

// 设置高亮状态 是否改变阴影

btn. reversesTitleShadowWhenHighlighted = 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, // 点击取消
    
    ... ...
}

获取

@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;             // 当前标题
@property(nonatomic,readonly,strong) UIColor  *currentTitleColor;                  // 当前标题颜色
@property(nullable, nonatomic,readonly,strong) UIColor  *currentTitleShadowColor;  // 当前标题阴影颜色
@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;             // 当前图片
@property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage;   // 当前背景图片
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0);  // 当前富文本标题

@property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel NS_AVAILABLE_IOS(3_0); // 当前标题UILabel
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView  NS_AVAILABLE_IOS(3_0); // 当前图片UIImageView
- (CGRect)backgroundRectForBounds:(CGRect)bounds;      // 返回背景绘制区域
- (CGRect)contentRectForBounds:(CGRect)bounds;         // 返回内容绘制区域
- (CGRect)titleRectForContentRect:(CGRect)contentRect; // 返回标题绘制区域
- (CGRect)imageRectForContentRect:(CGRect)contentRect; // 返回图片绘制区域

版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!

你可能感兴趣的:(iOS控件之UIButton)