代码搬运工这样就可以实现图片和文字同时显示
首先创建一个继承于UIButton的类
//1. .h中这样写
#import <UIKit/UIKit.h>
@interface SHJF_Button : UIButton
@end
#import "SHJF_Button.h"
@implementation SHJF_Button
重写init方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//可根据自己的需要随意调整
self . titleLabel . textAlignment = NSTextAlignmentLeft ;
self . titleLabel . font =[ UIFont systemFontOfSize : 14.0 ];
// self . titleLabel . backgroundColor = [UIColor orangeColor];
// self . imageView . contentMode = UIViewContentModeLeft ;
}
return self;
}
//重写父类UIButton的方法
//更具button的rect设定并返回文本label的rect
- ( CGRect )titleRectForContentRect:( CGRect )contentRect
{
CGFloat titleW = myWidth/3/5*3 ;
CGFloat titleH = myHeight/100*3 ;
CGFloat titleX = myWidth/3/5 ;
CGFloat titleY = myHeight/100*12.4 ;
contentRect = ( CGRect ){{titleX,titleY},{titleW,titleH}};
return contentRect;
}
//更具button的rect设定并返回UIImageView的rect
- ( CGRect )imageRectForContentRect:( CGRect )contentRect
{
CGFloat imageW = myWidth/100*10 ;
CGFloat imageH = myWidth/100*10 ;
CGFloat imageX = myWidth/100*11.5 ;
CGFloat imageY = myHeight/100*4.1 ;
contentRect = ( CGRect ){{imageX,imageY},{imageW,imageH}};
return contentRect;
}