UIButton默认显示图片和标题,如图:
此时,imageEdgeInsets(top,left,bottom,right) 和 titleEdgeInsets(top,left,bottom,right)默认是UIEdgeInsetsZero
可以进行2种方式的自定义:
- 1 ,图片在左标题在右
CGSize buttonTitleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
CGSize buttonImageSize = [self imageForState:UIControlStateSelected].size;
self.imageEdgeInsets = UIEdgeInsetsMake(0, buttonTitleSize.width+self.border,0, -buttonTitleSize.width-self.border);
self.titleEdgeInsets = UIEdgeInsetsMake(0, -buttonImageSize.width,0, buttonImageSize.width);
//self.border为自定义的图片和标题的间隔
2,图片在上标题在下
self.imageEdgeInsets=UIEdgeInsetsMake(-buttonTitleSize.height/2,buttonTitleSize.width/2, buttonTitleSize.height/2, -buttonTitleSize.width/2);
self.titleEdgeInsets=UIEdgeInsetsMake(buttonImageSize.height/2+self.border, -buttonImageSize.width/2, -buttonImageSize.height/2-self.border, buttonImageSize.width/2);