美文网首页
ios 自定义按钮

ios 自定义按钮

作者: 冷武橘 | 来源:发表于2022-03-26 10:38 被阅读0次

    害,长期苦于按钮久矣,每次都要图、文位置调整好麻烦、心累,所以自定义一个按钮吧

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface HLTButton : UIControl
    @property(nonatomic,strong)UILabel *lable;
    @property(nonatomic,strong)UIImageView *imagView;
    +(instancetype)layoutLeadingtitle:(NSString *)title  imageName:(NSString *)imageName axi:(UILayoutConstraintAxis)axi;
    
    +(instancetype)layoutLeadingimageName:(NSString *)imageName   title:(NSString *)title axi:(UILayoutConstraintAxis)axi;
    
    - (void)setTitle:(NSString *)title forState:(UIControlState)state;
    
    - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
    
    - (void)setImage:(NSString *)imageName forState:(UIControlState)state;
    
    @property(nonatomic,assign)CGSize intrinsicContentExtraSize;
    
    @property(nonatomic,assign) CGFloat spacing;
    
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    
    //
    //  HLTButton.m
    //  01-Autolayout
    //
    //  Created by HlT on 2022/3/25.
    //
    
    #import "HLTButton.h"
    @interface HLTButton()
    @property(nonatomic,strong)UIStackView *stackView;
    @property(nonatomic,strong)NSString *selectedText;
    @property(nonatomic,strong)NSString *normalText;
    @property(nonatomic,strong)UIColor *selectedTitleColor;
    @property(nonatomic,strong)UIColor *normalTitleColor;
    @property(nonatomic,strong)NSString *selectedImageName;
    @property(nonatomic,strong)NSString *normalImageName;
    @end
    @implementation HLTButton
    +(instancetype)layoutLeadingtitle:(NSString *)title  imageName:(NSString *)imageName axi:(UILayoutConstraintAxis)axi{
        HLTButton *btn =[[HLTButton alloc]init];
        btn.lable.text = title;
        btn.normalText = title;
        btn.selectedText = title;
        btn.imagView.image =[UIImage imageNamed:imageName];
        btn.selectedImageName = imageName;
        btn.normalImageName = imageName;
        [btn.stackView addArrangedSubview:btn.lable];
        [btn.stackView addArrangedSubview:btn.imagView];
        btn.stackView.axis = axi;
        return btn;
    }
    +(instancetype)layoutLeadingimageName:(NSString *)imageName   title:(NSString *)title axi:(UILayoutConstraintAxis)axi{
        HLTButton *btn =[[HLTButton alloc]init];
        btn.lable.text = title;
        btn.lable.text = title;
        btn.normalText = title;
        btn.selectedText = title;
        btn.imagView.image =[UIImage imageNamed:imageName];
        btn.selectedImageName = imageName;
        btn.normalImageName = imageName;
        [btn.stackView addArrangedSubview:btn.imagView];
        [btn.stackView addArrangedSubview:btn.lable];
        btn.stackView.axis = axi;
        return btn;
    }
    - (void)setTitle:(NSString *)title forState:(UIControlState)state{
        if (state==UIControlStateSelected) {
            self.selectedText = title;
            self.lable.text = self.selectedText;
        }else{
            self.normalText = title;
            self.lable.text = self.normalText;
        }
        if (self.selected==YES) {
            self.lable.text = self.selectedText;
        }else{
            self.lable.text = self.normalText;
        }
        [self.stackView setNeedsLayout];
        [self invalidateIntrinsicContentSize];
    }
    
    - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state{
        if (state==UIControlStateSelected) {
            self.selectedTitleColor = color;
            self.lable.textColor = self.selectedTitleColor;
        }else{
            self.normalTitleColor = color;
            self.lable.textColor = self.normalTitleColor;
        }
        if (self.selected==YES) {
            self.lable.textColor = self.selectedTitleColor;
        }else{
            self.lable.textColor = self.normalTitleColor;
        }
    }
    
    - (void)setImage:(NSString *)imageName forState:(UIControlState)state{
        if (state==UIControlStateSelected) {
            self.selectedImageName = imageName;
        }else{
            self.normalImageName = imageName;
        }
        
        if (self.selected==YES) {
            self.imagView.image =[UIImage imageNamed:self.selectedImageName];
        }else{
            self.imagView.image =[UIImage imageNamed:self.normalImageName];
        }
        [self.stackView setNeedsLayout];
        [self invalidateIntrinsicContentSize];
    }
    
    
    - (void)setSelected:(BOOL)selected{
        [super setSelected:selected];
        if (selected==YES) {
            self.lable.text = self.selectedText;
            self.lable.textColor = self.selectedTitleColor;
            self.imagView.image =[UIImage imageNamed:self.selectedImageName];
        }else{
            self.lable.text = self.normalText;
            self.lable.textColor = self.normalTitleColor;
            self.imagView.image =[UIImage imageNamed:self.normalImageName];
        }
        [self.stackView setNeedsLayout];
        [self invalidateIntrinsicContentSize];
    }
    
    - (instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            UIStackView *stackview =[[UIStackView alloc]init];
            [self addSubview:stackview];
            stackview.userInteractionEnabled = NO;
            stackview.translatesAutoresizingMaskIntoConstraints = NO;
            [[stackview.centerYAnchor constraintEqualToAnchor:self.centerYAnchor]setActive:YES];
            [[stackview.centerXAnchor constraintEqualToAnchor:self.centerXAnchor]setActive:YES];
            self.lable =[[UILabel alloc]init];
            self.normalTitleColor = self.lable.textColor;
            self.selectedTitleColor = self.lable.textColor;
            stackview.alignment= UIStackViewAlignmentCenter;
            self.imagView.userInteractionEnabled = YES;
           self.imagView =[[UIImageView alloc]init];
            self.stackView = stackview;
        }
        return self;
    }
    - (CGSize)intrinsicContentSize{
      [self.stackView layoutIfNeeded];
       CGFloat  w = self.stackView.bounds.size.width+self.intrinsicContentExtraSize.width;
       CGFloat  h = self.stackView.bounds.size.height+self.intrinsicContentExtraSize.height;
        
       return CGSizeMake(w, h);
        
    }
    
    - (void)setSpacing:(CGFloat)spacing{
        _spacing =spacing;
        self.stackView.spacing = spacing;
        [self invalidateIntrinsicContentSize];
    }
    
    @end
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
      
        //1、水平方式排布:UILayoutConstraintAxisHorizontal
        //[self test1];//左图、右文字
        
        //[self test2];//左文、右图
    
        
        //2、水平方式排布:UILayoutConstraintAxisHorizontal
        //[self test3];//上文字、下图
        
         // [self test4];上图、下文字
        
        
        //3、其它基本设置
        HLTButton *btn =[HLTButton layoutLeadingimageName:@"unselect" title:@"点我啊" axi:UILayoutConstraintAxisHorizontal];
       [btn setTitle:@"来点我啊" forState:UIControlStateSelected];
        btn.lable.font = [UIFont systemFontOfSize:12];
        btn.spacing = 10;
        btn.backgroundColor =[UIColor yellowColor];
        [self.view addSubview:btn];
        
        //有外部约束确定大小
    //    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerY.equalTo(self.view);
    //        make.left.offset(100);
    //        make.right.offset(-100);
    //        make.height.equalTo(@300);
    //    }];
        
        //当然也可以用framae
        //btn.frame = CGRectMake(100, 100, 100, 300);
        
        
        
        
        //没有外部大小,可以从本身有固有大小自适应
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view);
        }];
    //
       
        
        //给固有大小增加额外的大小,保证左右两边有一定间隙
        //btn.intrinsicContentExtraSize = CGSizeMake(30, 0);
        
     
        [btn setImage:@"select" forState:UIControlStateSelected];
        
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    - (void)click:(UIControl *)sender{
        sender.selected=!sender.selected;
    }
    

    相关文章

      网友评论

          本文标题:ios 自定义按钮

          本文链接:https://www.haomeiwen.com/subject/ulpncrtx.html