UIButton默认显示图片和标题,如图:
2E7371BD-9436-4835-8F05-D74148E9E97C.png此时,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为自定义的图片和标题的间隔
45016748-9DFD-47E9-8B02-8342026A8378.png
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);
FDCD377B-5E25-442E-8FFA-17AE3C2D25A7.png
网友评论