封装实现UIButton左文字右图片

时间:2022-03-14 16:21:22

#import "TitleButton.h"

@implementation TitleButton

- (instancetype)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame]) {

[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

self.titleLabel.font = [UIFont boldSystemFontOfSize:16];

   [self setTitle:@"title" forState:UIControlStateNormal];

[self setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];

[self setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];

}

return self;

}

/** 设置内部imageView的frame */

//- (CGRect)imageRectForContentRect:(CGRect)contentRect

//{

//    CGFloat x = ;

//    CGFloat y = ;

//    CGFloat width = ;

//    CGFloat height = ;

//    return CGRectMake(x, y, width, height)

//}

/** 设置内部titleLabel的frame */

//- (CGRect)titleRectForContentRect:(CGRect)contentRect

//{

//    CGFloat x = ;

//    CGFloat y = ;

//    CGFloat width = ;

//    CGFloat height = ;

//    return CGRectMake(x, y, width, height)

//

//}

- (void)layoutSubviews

{

[super layoutSubviews];

//titleLable的x

self.titleLabel.x = 0;

//imageView的x

self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) ;

}