ios 自定義按鈕

害,長期苦于按鈕久矣,每次都要圖诱建、文位置調(diào)整好麻煩稀并、心累仅颇,所以自定義一個按鈕吧

#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、其它基本設(shè)置
    HLTButton *btn =[HLTButton layoutLeadingimageName:@"unselect" title:@"點(diǎn)我啊" axi:UILayoutConstraintAxisHorizontal];
   [btn setTitle:@"來點(diǎn)我啊" 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);
//    }];
    
    //當(dāng)然也可以用framae
    //btn.frame = CGRectMake(100, 100, 100, 300);
    
    
    
    
    //沒有外部大小蝙场,可以從本身有固有大小自適應(yīng)
    [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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市售滤,隨后出現(xiàn)的幾起案子罚拟,更是在濱河造成了極大的恐慌,老刑警劉巖完箩,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赐俗,死亡現(xiàn)場離奇詭異,居然都是意外死亡弊知,警方通過查閱死者的電腦和手機(jī)阻逮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秩彤,“玉大人叔扼,你說我怎么就攤上這事÷祝” “怎么了瓜富?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長珊拼。 經(jīng)常有香客問我食呻,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任仅胞,我火速辦了婚禮每辟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘干旧。我一直安慰自己渠欺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布椎眯。 她就那樣靜靜地躺著挠将,像睡著了一般。 火紅的嫁衣襯著肌膚如雪编整。 梳的紋絲不亂的頭發(fā)上舔稀,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天,我揣著相機(jī)與錄音掌测,去河邊找鬼内贮。 笑死,一個胖子當(dāng)著我的面吹牛汞斧,可吹牛的內(nèi)容都是我干的夜郁。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼粘勒,長吁一口氣:“原來是場噩夢啊……” “哼竞端!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起庙睡,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤事富,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后乘陪,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體赵颅,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年暂刘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捂刺。...
    茶點(diǎn)故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡谣拣,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出族展,到底是詐尸還是另有隱情森缠,我是刑警寧澤,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布仪缸,位于F島的核電站贵涵,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜宾茂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一瓷马、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧跨晴,春花似錦欧聘、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至焕妙,卻和暖如春蒋伦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背焚鹊。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工痕届, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人寺旺。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓爷抓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親阻塑。 傳聞我的和親對象是個殘疾皇子蓝撇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容