封装实现UIButton左文字右图片

#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;

}

 

/** 设置内部imageViewframe */

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

//{

//    CGFloat x = ;

//    CGFloat y = ;

//    CGFloat width = ;

//    CGFloat height = ;

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

//}

/** 设置内部titleLabelframe */

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

//{

//    CGFloat x = ;

//    CGFloat y = ;

//    CGFloat width = ;

//    CGFloat height = ;

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

//    

//}

- (void)layoutSubviews

{

    [super layoutSubviews]; 

    //titleLablex

    self.titleLabel.x = 0;

    //imageViewx

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

}

原文地址:https://www.cnblogs.com/starainDou/p/5183180.html