介紹兩種管理UIButton中的Image和Title的方法
1憨琳、自定義UIButton
#import "BBImageButton.h"
@implementation BBImageButton
- (instancetype)initWithTitle:(NSString *)title
{
self = [super initWithFrame:CGRectZero];
if (self) {
[self setTitle:title forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];
// 設(shè)置button為Selected狀態(tài)時坯苹,點擊button時篷角,圖片不出現(xiàn)閃動
[self setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:(UIControlStateSelected | UIControlStateHighlighted)];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.imageView.frame), CGRectGetHeight(self.imageView.frame));
self.imageView.center = CGPointMake(CGRectGetWidth(self.frame) / 2, self.imageView.center.y);
self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame) + 10, CGRectGetWidth(self.titleLabel.frame), CGRectGetHeight(self.titleLabel.frame));
self.titleLabel.center = CGPointMake(self.imageView.center.x, self.titleLabel.center.y);
}
@end
如下圖所示:
1.png
左右布局
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat x = CGRectGetWidth(self.frame) * 0.5 - (CGRectGetWidth(self.titleLabel.frame) - CGRectGetWidth(self.imageView.frame));
self.titleLabel.frame = CGRectMake(x, self.titleLabel.frame.origin.y, CGRectGetWidth(self.titleLabel.frame), CGRectGetHeight(self.titleLabel.frame));
self.imageView.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame), self.imageView.frame.origin.y, CGRectGetWidth(self.imageView.frame), CGRectGetHeight(self.imageView.frame));
}
如下圖所示:
2.png
在這里面有個小技巧逛钻,如果想要文字跟圖片有一定間距的話,不一定非得改變它的坐標芜壁,可以在title賦值的時候這樣處理功茴,“加一個空格”庐冯,作為程序猿,我們不應(yīng)該放棄任何一個偷懶的機會孽亲。
title = [NSString stringWithFormat:@"%@ ", title];
如下圖所示:
3.png
2坎穿、UIButton中的titleEdgeInsets和imageEdgeInsets可以管理button中image和title的布局。
默認情況下,是圖片在左,文字在右,垂直居中顯示,如下圖:
button.titleEdgeInsets = UIEdgeInsetsZero;
button.imageEdgeInsets = UIEdgeInsetsZero;
11.png
設(shè)置如下布局后,圖片和文字都居中顯示返劲。
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, 0, 0);
// button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, button.titleLabel.frame.size.width);
// 由于iOS8中titleLabel的size為0玲昧,用上面這樣設(shè)置有問題,修改一下即可
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -button.titleLabel.intrinsicContentSize.width);
22.png
如果想圖片在上,文字在下,水平居中顯示,則按下面設(shè)置即可:
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, -button.imageView.frame.size.height, 0);
// button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.frame.size.height, 0, 0, -button.titleLabel.frame.size.width);
// 由于iOS8中titleLabel的size為0篮绿,用上面這樣設(shè)置有問題孵延,修改一下即可
button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.intrinsicContentSize.height, 0, 0, -button.titleLabel.intrinsicContentSize.width);
33.png
如果覺得圖片和文字離的太近了,稍微分開一點:
CGFloat offset = 40.0f;
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, -button.imageView.frame.size.height-offset/2, 0);
// button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.frame.size.height-offset/2, 0, 0, -button.titleLabel.frame.size.width);
// 由于iOS8中titleLabel的size為0亲配,用上面這樣設(shè)置有問題尘应,修改一下即可
button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.intrinsicContentSize.height-offset/2, 0, 0, -button.titleLabel.intrinsicContentSize.width);
44.png
文字左對齊,圖片右對齊
button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width - button.frame.size.width + button.titleLabel.intrinsicContentSize.width, 0, 0);
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -button.titleLabel.frame.size.width - button.frame.size.width + button.imageView.frame.size.width);
55.png