UIButton image位置的调节
调节image的位置主要是通过 UIButton的imageEdgeInsets和titleEdgeInsets这两个属性来实现的
CGFloat imageWidth = self.imageView.bounds.size.width;
CGFloat imageHeight = self.imageView.bounds.size.height;
CGFloat titleWidth = self.titleLabel.bounds.size.width;
CGFloat titleHeight = self.titleLabel.bounds.size.height;
CGFloat insetAmount = space / 2;
CGFloat width = self.frame.size.width;
- 图片居左,调整 image 和 文字之间的间距 (space 是两者之间的间距)
self.imageEdgeInsets = UIEdgeInsetsMake(0,-insetAmount, 0, insetAmount);
self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount);
- 图片居右,调整 image 和 文字之间的间距
self.imageEdgeInsets = UIEdgeInsetsMake(0,(titleWidth + insetAmount), 0, -(titleWidth + insetAmount));
self.titleEdgeInsets = UIEdgeInsetsMake(0,-(imageWidth + insetAmount), 0, (imageWidth + insetAmount));
- 图片居上,调整 image 和 文字之间的间距
self.imageEdgeInsets = UIEdgeInsetsMake(-titleHeight - insetAmount,(width - imageWidth)/2, 0, (width - imageWidth)/2 - titleWidth);
self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageWidth - insetAmount, 0);
- 图片居下,调整 image 和 文字之间的间距
self.imageEdgeInsets = UIEdgeInsetsMake(0, (width - imageWidth)/2 , -titleHeight - insetAmount, (width - imageWidth)/2 - titleWidth);
self.titleEdgeInsets = UIEdgeInsetsMake(-imageHeight - insetAmount, -imageWidth, 0, 0);
OC 的实现
typedef NS_ENUM(NSUInteger,ImageLocation) {
ImageLocationLeft = 0,
ImageLocationRight,
ImageLocationTop,
ImageLocationBottom};
- (void)setImage:(ImageLocation)location space:(CGFloat)space{
CGFloat imageWidth = self.imageView.bounds.size.width;
CGFloat imageHeight = self.imageView.bounds.size.height;
CGFloat titleWidth = self.titleLabel.bounds.size.width;
CGFloat titleHeight = self.titleLabel.bounds.size.height;
CGFloat insetAmount = space / 2;
CGFloat width = self.frame.size.width;
if (location == ImageLocationLeft) {
self.imageEdgeInsets = UIEdgeInsetsMake(0,-insetAmount, 0, insetAmount);
self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount);
}else if (location == ImageLocationRight){
self.imageEdgeInsets = UIEdgeInsetsMake(0,(titleWidth + insetAmount), 0, -(titleWidth + insetAmount));
self.titleEdgeInsets = UIEdgeInsetsMake(0,-(imageWidth + insetAmount), 0, (imageWidth + insetAmount));
}else if (location == ImageLocationTop){
self.imageEdgeInsets = UIEdgeInsetsMake(-titleHeight - insetAmount,(width - imageWidth)/2, 0, (width - imageWidth)/2 - titleWidth);
self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageWidth - insetAmount, 0);
}else if (location == ImageLocationBottom){
self.imageEdgeInsets = UIEdgeInsetsMake(0, (width - imageWidth)/2 , -titleHeight - insetAmount, (width - imageWidth)/2 - titleWidth);
self.titleEdgeInsets = UIEdgeInsetsMake(-imageHeight - insetAmount, -imageWidth, 0, 0);
}
}
Swift实现
public enum ImageLocation{
case left,right,top,bottom
}
/// 设置image位置(必须要先设置frame)
/// - Parameters:
/// - location: 位置
/// - space: 标题与图片的间距
func setImage(location:ImageLocation,space:CGFloat){
guard let label = titleLabel,let imageV = imageView else {
return
}
let imageWidth = imageV.bounds.size.width
let imageHeight = imageV.bounds.size.height
let titleWidth = label.bounds.size.width
let titleHeight = label.bounds.size.height
let width = frame.size.width
switch location {
case .left:
titleEdgeInsets = UIEdgeInsets.init(top: 0, left: space/2, bottom: 0, right: -space/2)
imageEdgeInsets = UIEdgeInsets.init(top: 0, left: -space/2, bottom: 0, right: space/2)
case .right:
imageEdgeInsets = UIEdgeInsets.init(top: 0, left: (titleWidth + space/2), bottom: 0, right: -(titleWidth + space/2))
titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -(imageWidth + space/2), bottom: 0, right: (imageWidth + space/2))
case .top:
imageEdgeInsets = UIEdgeInsets.init(top:-titleHeight - space/2, left:(width - imageWidth)/2, bottom: 0, right: (width - imageWidth)/2 - titleWidth)
titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -imageWidth, bottom: -imageHeight - space/2, right: 0)
case .bottom:
imageEdgeInsets = UIEdgeInsets.init(top:0, left: (width - imageWidth)/2, bottom: -titleHeight - space/2, right: (width - imageWidth)/2 - titleWidth)
titleEdgeInsets = UIEdgeInsets.init(top: -imageHeight - space/2, left: -imageWidth, bottom: 0, right: 0)
}
}
如果没有提前设置frame的是无法获取到title的宽度和高度的,如果通过Snapkit或者Masonry 布局的话,可以提前设置size,或者在布局结束后再设置image的位置
网友评论