定制一个image 和title 有一定距离的UIButton
// the space between the image and text
CGFloat spacing = 6.0;// lower the text and push it left so it appears centered
// below the image
CGSize imageSize = button.imageView.image.size;
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);// raise the image and push it right so it appears centered
// above the text
CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: button.titleLabel.font}];
button.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);
UIButton的setBackgroundImage
是设置背景图片,而setImage
则是设置一个挨着title的一个icon 或者image。
就是如果你设置的是backgroundImage 而不是image的话,那么如果这个图片过小的话这个图片是会被拉伸的。