害,長期苦于按鈕久矣,每次都要圖诱建、文位置調(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;
}