- (void)setImageOnRightWithImage:(UIImage *)image forState:(UIControlState)state
{
[self setImage:image forState:state];
NSAttributedString *title = [[self titleForState:state] length] > 0 ? [[NSAttributedString alloc] initWithString:[self titleForState:state]] : [self attributedTitleForState:state];
CGSize textSize = title.size;
[self setImageEdgeInsets:UIEdgeInsetsMake(0, textSize.width, 0, -textSize.width)];
[self setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width, 0, image.size.width)];
[self setContentEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
}
在使用这个方法前,需要设置btn的title或者attributedTitle,尽量使用attributedTitle;
调整完image和title的edgeInsets后,btn本省的edgeInsets会被置成zero,需重新设置新的edgeInsets