self.btn.titleLabel.backgroundColor = [UIColor clearColor];
self.btn.imageView.backgroundColor = [UIColor clearColor];
CGSize titleSize = self.btn.titleLabel.bounds.size;
CGSize imageSize = self.btn.imageView.bounds.size;
// self.btn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 10, 10);
CGFloat spacing = 70;
// 图左文右
self.btn.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
self.btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
// 文左图右
CGFloat targetTitleX = (self.btn.bounds.size.width - titleSize.width - imageSize.width)/2.0;
CGFloat targetImageX = targetTitleX+titleSize.width;
self.btn.titleEdgeInsets = UIEdgeInsetsMake(0, targetTitleX - self.btn.titleLabel.frame.origin.x-spacing/2.0, 0, -(targetTitleX - self.btn.titleLabel.frame.origin.x)+spacing/2.0);
self.btn.imageEdgeInsets = UIEdgeInsetsMake(0, targetImageX-self.btn.imageView.frame.origin.x+spacing/2.0, 0, -(targetImageX-self.btn.imageView.frame.origin.x)-spacing/2.0);
// 图上文下
self.btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, titleSize.height, -titleSize.width);
self.btn.titleEdgeInsets = UIEdgeInsetsMake(imageSize.height, -imageSize.width, 0, 0);
不够用就参考https://www.jianshu.com/p/4676d84458f7
网友评论