以下代碼是筆者再工作中總結(jié)得來的,如果錯誤澳迫,請多多指教动知!
給按鈕創(chuàng)建個分類UIButton+EdgeInsets
,然后將代碼拷貝到對應(yīng)文件即可
- UIButton+EdgeInsets.h文件
/**
圖片在左,文字在右
@param space 圖片和文字的間隔
*/
- (void)imageOnLeftWithSpace:(CGFloat)space;
/**
圖片在右神得,文字在左
@param space 圖片和文字的間隔
*/
- (void)imageOnRightWithSpace:(CGFloat)space;
/**
圖片在上,文字在下
@param space 圖片和文字的間隔
*/
- (void)imageOnTopWithSpace:(CGFloat)space;
/**
圖片在下饥侵,文字在上
@param space 圖片和文字的間隔
*/
- (void)imageOnBottomWithSpace:(CGFloat)space;
/**
用于有圖片有文字的按鈕鸵赫。
功能:設(shè)置圖片和文字之間的間距,并且可設(shè)置內(nèi)容距離父視圖左邊的間距
@param space1 圖片和文字之家的間距
@param space2 內(nèi)容距離父視圖左邊的間距
*/
- (void)imageOnLeftWithSpace:(CGFloat)space1 contentLeftSpace:(CGFloat)space2;
- UIButton+EdgeInsets.m文件
- (void)imageOnLeftWithSpace:(CGFloat)space
{
[self layoutIfNeeded];
self.titleEdgeInsets
= UIEdgeInsetsMake(
0,// top
space * 0.5,// left
0,// bottom
- space * 0.5);// right
self.imageEdgeInsets
= UIEdgeInsetsMake(
0,// top
- space * 0.5,// left
0,// bottom
space * 0.5);// right
}
- (void)imageOnRightWithSpace:(CGFloat)space
{
[self layoutIfNeeded];
self.titleEdgeInsets
= UIEdgeInsetsMake(0,// top
- self.imageView.bounds.size.width - space * 0.5,// left
0,// bottom
self.imageView.bounds.size.width + space * 0.5);// right
self.imageEdgeInsets
= UIEdgeInsetsMake(0,// top
self.titleLabel.bounds.size.width + space * 0.5,// left
0,// bottom
- self.titleLabel.bounds.size.width - space * 0.5);// right
}
- (void)imageOnTopWithSpace:(CGFloat)space
{
[self layoutIfNeeded];
BOOL holisticUpMove = self.titleLabel.bounds.size.height > self.imageView.bounds.size.height;
CGFloat offset = fabs(self.imageView.bounds.size.height - self.titleLabel.bounds.size.height) / 2;
CGFloat imageTop = 0;
CGFloat imageLeft = 0;
CGFloat imageBottom = 0;
CGFloat imageRight = 0;
if (holisticUpMove) {
// 整體上移
imageTop = -self.imageView.bounds.size.height * 0.5 - offset - space * 0.5;
imageLeft = self.titleLabel.bounds.size.width * 0.5;
imageBottom = self.imageView.bounds.size.height * 0.5 + offset + space * 0.5;
imageRight = - self.titleLabel.bounds.size.width * 0.5;
}else {
// 整體下移
imageTop = -self.imageView.bounds.size.height * 0.5 + offset - space * 0.5;
imageLeft = self.titleLabel.bounds.size.width * 0.5;
imageBottom = self.imageView.bounds.size.height * 0.5 - offset + space * 0.5;
imageRight = - self.titleLabel.bounds.size.width * 0.5;
}
self.imageEdgeInsets
= UIEdgeInsetsMake(
imageTop,// top
imageLeft,// left
imageBottom,// bottom
imageRight);// right
CGFloat labelTop = 0;
CGFloat labelLeft = 0;
CGFloat labelBottom = 0;
CGFloat labelRight = 0;
if (holisticUpMove) {
// 整體上移
labelTop = self.titleLabel.bounds.size.height * 0.5 - offset + space * 0.5;
labelLeft = - self.imageView.bounds.size.width * 0.5;
labelBottom = -self.titleLabel.bounds.size.height * 0.5 + offset - space * 0.5;
labelRight = self.imageView.bounds.size.width * 0.5;
}else {
// 整體下移
labelTop = self.titleLabel.bounds.size.height * 0.5 + offset + space * 0.5;
labelLeft = - self.imageView.bounds.size.width * 0.5;
labelBottom = -self.titleLabel.bounds.size.height * 0.5 - offset - space * 0.5;
labelRight = self.imageView.bounds.size.width * 0.5;
}
self.titleEdgeInsets
= UIEdgeInsetsMake(
labelTop,// top
labelLeft,// left
labelBottom,// bottom
labelRight);// right
}
- (void)imageOnBottomWithSpace:(CGFloat)space
{
[self layoutIfNeeded];
BOOL holisticUpMove = self.imageView.bounds.size.height > self.titleLabel.bounds.size.height;
CGFloat offset = fabs(self.imageView.bounds.size.height - self.titleLabel.bounds.size.height) / 2;
CGFloat imageTop = 0;
CGFloat imageLeft = 0;
CGFloat imageBottom = 0;
CGFloat imageRight = 0;
if (holisticUpMove) {
// 整體需要上移
imageTop = self.imageView.bounds.size.height * 0.5 - offset + space * 0.5;
imageLeft = self.titleLabel.bounds.size.width * 0.5;
imageBottom = - self.imageView.bounds.size.height * 0.5 + offset - space * 0.5;
imageRight = - self.titleLabel.bounds.size.width * 0.5;
}else {
// 整體需要下移
imageTop = self.imageView.bounds.size.height * 0.5 + offset + space * 0.5;
imageLeft = self.titleLabel.bounds.size.width * 0.5;
imageBottom = - self.imageView.bounds.size.height * 0.5 - offset - space * 0.5;
imageRight = - self.titleLabel.bounds.size.width * 0.5;
}
self.imageEdgeInsets
= UIEdgeInsetsMake(
imageTop,// top
imageLeft,// left
imageBottom,// bottom
imageRight);// right
CGFloat labelTop = 0;
CGFloat labelLeft = 0;
CGFloat labelBottom = 0;
CGFloat labelRight = 0;
if (holisticUpMove) {
// 整體上移
labelTop = - self.titleLabel.bounds.size.height * 0.5 - offset - space * 0.5;
labelLeft = - self.imageView.bounds.size.width * 0.5;
labelBottom = self.titleLabel.bounds.size.height * 0.5 + offset + space * 0.5;
labelRight = self.imageView.bounds.size.width * 0.5;
}else {
// 整體下移
labelTop = - self.titleLabel.bounds.size.height * 0.5 + offset - space * 0.5;
labelLeft = - self.imageView.bounds.size.width * 0.5;
labelBottom = self.titleLabel.bounds.size.height * 0.5 - offset + space * 0.5;
labelRight = self.imageView.bounds.size.width * 0.5;
}
self.titleEdgeInsets
= UIEdgeInsetsMake(
labelTop,// top
labelLeft,// left
labelBottom,// bottom
labelRight);// right
}
- (void)imageOnLeftWithSpace:(CGFloat)space1 contentLeftSpace:(CGFloat)space2
{
[self imageOnLeftWithSpace:space1];
CGFloat insetLeft = self.imageView.frame.origin.x - space2;
self.contentEdgeInsets = UIEdgeInsetsMake(0, - insetLeft, 0, insetLeft);
}