ios

UIButton的使用

2017-08-22  本文已影响6人  风轻鱼蛋

1、简单介绍UIButton

UIButton是继承于UIControl(UIControl继承于UIView),UIButton具有点击功能,可以绑定事件。UIButton既能显示文字,又能显示图片,还能随时调整内部文字和图片的位置。

2、UIButton使用及常用属性

- (UIButton *)textButton
{
    if (!_textButton) {
        //1、初始化button
        _textButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //2、设置frame
        _textButton.frame = CGRectMake(100, 100, 200, 50);
        //3、设置背景颜色
        _textButton.backgroundColor = [UIColor blueColor];
        //4、设置title
        _textButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_textButton setTitle:@"正常状态下的title" forState:UIControlStateNormal];
        [_textButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [_textButton setTitle:@"选中状态下的title" forState:UIControlStateSelected];
        [_textButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        //5、设置Image
        [_textButton setImage:[UIImage imageNamed:@"Image-1"] forState:UIControlStateNormal];
        [_textButton setImage:[UIImage imageNamed:@"Image-2"] forState:UIControlStateSelected];
        //6、添加事件
        [_textButton addTarget:self action:@selector(textButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:_textButton];
    }
    return _textButton;
}
//常用属性
@property(nonatomic,readonly) UIButtonType buttonType;  //按钮形状类型

@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 ;  
@property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel;
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView ;  

@property(nonatomic,readonly) UIControlState  //按钮状态类型
/* 
UIControlStateNormal = 0, 常规状态显现
UIControlStateHighlighted = 1 << 0, 高亮状态显现
UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
UIControlStateSelected = 1 << 2, 选中状态
UIControlStateApplication = 0x00FF0000, 当应用程序标志时
UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
*/
//特殊属性
@property(nonatomic) UIEdgeInsets titleEdgeInsets;    //设置标题的边缘值
@property(nonatomic) UIEdgeInsets imageEdgeInsets; //设置图片的边缘值
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted;  // 点击按钮是否使其发光
@property(nonatomic) BOOL adjustsImageWhenHighlighted;// 高亮时是否显示图像的变化。
@property(nonatomic) BOOL adjustsImageWhenDisabled; //禁用时是否显示图像的变化
@property(nonatomic)  BOOL  showsTouchWhenHighlighted; //点击按钮时是否发光

@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;    //按钮垂直放置方式
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment; //按钮水平放置方式

3、UIButton内部文字和图片的位置调整

    self.textButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    self.textButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);

4、自定义Button内部文字和图片的位置调整

//重写下面方法 内部布局
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
- (CGRect)contentRectForBounds:(CGRect)bounds;
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
- (CGRect)imageRectForContentRect:(CGRect)contentRect;

上一篇下一篇

猜你喜欢

热点阅读