- (void)imagePosition:(BSYButtonImageEdgeInsetsStyle)style space:(CGFloat)space {
if(self.imageView==nil) {
return;
}
if(self.titleLabel==nil) {
return;
}
//获取图像的宽和高
CGFloat imageWidth = self.imageView.frame.size.width;
CGFloat imageHeight = self.imageView.frame.size.height;
//获取文字的宽和高
CGFloatlabelWidth =self.titleLabel.intrinsicContentSize.width;
CGFloat labelHeight = self.titleLabel.intrinsicContentSize.height;
UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
//UIButton同时有图像和文字的正常状态---左图像右文字,间距为0
switch(style) {
caseLEFT:
imageEdgeInsets =UIEdgeInsetsMake(0, -space*0.5,0, space*0.5);
labelEdgeInsets =UIEdgeInsetsMake(0, space*0.5,0, -space*0.5);
break;
caseRIGHT:
imageEdgeInsets =UIEdgeInsetsMake(0,labelWidth + space *0.5,0, -labelWidth - space *0.5);
labelEdgeInsets =UIEdgeInsetsMake(0, -imageWidth - space *0.5,0, imageWidth + space *0.5);
break;
caseTOP:
imageEdgeInsets =UIEdgeInsetsMake( -imageHeight *0.5- space *0.5,labelWidth *0.5, imageHeight *0.5+ space *0.5, -labelWidth *0.5);
labelEdgeInsets =UIEdgeInsetsMake(labelHeight *0.5+ space *0.5, -imageWidth *0.5, -labelHeight *0.5- space *0.5, imageWidth *0.5);
break;
caseBOTTOM:
imageEdgeInsets =UIEdgeInsetsMake(imageHeight *0.5+ space *0.5,labelWidth *0.5, -imageHeight *0.5- space *0.5,-labelWidth *0.5);
labelEdgeInsets =UIEdgeInsetsMake(-labelHeight *0.5- space *0.5, -imageWidth *0.5, labelHeight *0.5+ space *0.5, imageWidth *0.5);
break;
default:
break;
}
self.titleEdgeInsets= labelEdgeInsets;
self.imageEdgeInsets= imageEdgeInsets;
}
网友评论