ios button 上的文字和圖片的位置顯示

一。創建不同的一個分類打補丁 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];


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容