一。创建不同的一个分类打补丁 button文字图片显示类型
typedefNS_ENUM(NSUInteger, MKButtonEdgeInsetsStyle) {
MKButtonEdgeInsetsStyleTop,// image在上,label在下
MKButtonEdgeInsetsStyleLeft,// image在左,label在右
MKButtonEdgeInsetsStyleBottom,// image在下,label在上
MKButtonEdgeInsetsStyleRight// image在右,label在左
};
二。 //.m 里实现方法方法
- (void)layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space
{
// 1.得到imageView和titleLabel的宽、高
CGFloatimageWith =self.imageView.frame.size.width;
CGFloatimageHeight =self.imageView.frame.size.height;
CGFloatlabelWidth =0.0;
CGFloatlabelHeight =0.0;
if([UIDevicecurrentDevice].systemVersion.floatValue>=8.0) {
//由于iOS8中titleLabel的size为0,用下面的这种设置
labelWidth =self.titleLabel.intrinsicContentSize.width;
labelHeight =self.titleLabel.intrinsicContentSize.height;
}else{
labelWidth =self.titleLabel.frame.size.width;
labelHeight =self.titleLabel.frame.size.height;
}
// 2.声明全局的imageEdgeInsets和labelEdgeInsets
UIEdgeInsetsimageEdgeInsets =UIEdgeInsetsZero;
UIEdgeInsetslabelEdgeInsets =UIEdgeInsetsZero;
// 3.根据style和space得到imageEdgeInsets和labelEdgeInsets的值
switch(style) {
caseMKButtonEdgeInsetsStyleTop:
{
imageEdgeInsets =UIEdgeInsetsMake(-labelHeight-space/2.0,0,0, -labelWidth);
labelEdgeInsets =UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0,0);
}
break;
caseMKButtonEdgeInsetsStyleLeft:
{
imageEdgeInsets =UIEdgeInsetsMake(0, -space/2.0,0, space/2.0);
labelEdgeInsets =UIEdgeInsetsMake(0, space/2.0,0, -space/2.0);
}
break;
caseMKButtonEdgeInsetsStyleBottom:
{
imageEdgeInsets =UIEdgeInsetsMake(0,0, -labelHeight-space/2.0, -labelWidth);
labelEdgeInsets =UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith,0,0);
}
break;
caseMKButtonEdgeInsetsStyleRight:
{
imageEdgeInsets =UIEdgeInsetsMake(0, labelWidth+space/2.0,0, -labelWidth-space/2.0);
labelEdgeInsets =UIEdgeInsetsMake(0, -imageWith-space/2.0,0, imageWith+space/2.0);
}
break;
default:
break;
}
// 4.赋值
self.titleEdgeInsets= labelEdgeInsets;
self.imageEdgeInsets= imageEdgeInsets;
}
三 。最后调用:首先创建,设置好image 和 title 即使title 是动态的宽度也OK的 我举得例子是动态宽度的title
UIButton*button = [[UIButtonalloc]init];
CGFloatspace =8;
NSString*textString =self.AllBtns[i];
button.titleLabel.font= [UIFontsystemFontOfSize:14];
//求动态宽度
CGRectrect= [ToolsJJdynamicHeight:textStringsize:14width:20000height:20];
CGFloat width = rect.size.width;
//你可以给width换一个固定的值
button.frame=CGRectMake(labelX,0,width,50);
button.backgroundColor= [UIColoryellowColor];
labelX = labelX + width+20;
[buttonsetTitle:textStringforState:UIControlStateNormal];
[buttonsetImage:[UIImageimageNamed:@"set"]forState:UIControlStateNormal];
//调用分类方法
[buttonlayoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleBottomimageTitleSpace:space];
四 。 不是动态宽度固定宽度的button也是可以的
UIButton*button = [[UIButtonalloc]init];
CGFloatspace =8;
NSString*textString =@"wwwww";
button.titleLabel.font= [UIFontsystemFontOfSize:14];
//CGRect rect= [ToolsJJ dynamicHeight:textString size:14 width:20000 height:20];
//CGFloat width = rect.size.width;
//
button.frame=CGRectMake(labelX,0,80,50);
button.backgroundColor= [UIColoryellowColor];
//labelX = labelX + width+20;
[buttonsetTitle:textStringforState:UIControlStateNormal];
[buttonsetImage:[UIImageimageNamed:@"set"]forState:UIControlStateNormal];
[buttonlayoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleBottomimageTitleSpace:space];