UIButton 中图片和文字位置调整解决方法
2019-10-31 本文已影响0人
Zcocoa
#import "UIButton+Add.h"
@implementation UIButton (Add)
- (void)setIconToTopWithSpace:(CGFloat)spacing {
CGSize imageSize = self.imageView.image.size;
self.titleEdgeInsets = UIEdgeInsetsMake(
0.0, - imageSize.width, - (imageSize.height+ spacing),0.0);
CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: self.titleLabel.font}];
self.imageEdgeInsets = UIEdgeInsetsMake(
- (titleSize.height+ spacing),0.0,0.0, - titleSize.width);
CGFloatedgeOffset =fabs(titleSize.height- imageSize.height) /2.0;
self.contentEdgeInsets=UIEdgeInsetsMake(edgeOffset,0.0, edgeOffset,0.0);
}
- (void)setIconToRightWithSpace:(CGFloat)spacing {
CGSize imageSize = self.imageView.image.size;
self.titleEdgeInsets = UIEdgeInsetsMake(
0.0, -(imageSize.width+ spacing),0, imageSize.width);
CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: self.titleLabel.font}];
self.imageEdgeInsets = UIEdgeInsetsMake(
0, (titleSize.width+ spacing),0.0, -titleSize.width);
CGFloatedgeOffset =fabs(titleSize.height- imageSize.height) /2.0;
self.contentEdgeInsets=UIEdgeInsetsMake(edgeOffset,0.0, edgeOffset,0.0);
}
- (void)setIconToLetfWithSpace:(CGFloat)spacing{
self.titleEdgeInsets = UIEdgeInsetsMake(
0.0, spacing,0.0,0.0);
self.imageEdgeInsets = UIEdgeInsetsMake(
0.0, - spacing,0.0,0.0);
}
- (void)setIconToBottomWithSpace:(CGFloat)spacing{
CGSize imageSize = self.imageView.image.size;
self.titleEdgeInsets = UIEdgeInsetsMake(
0, -imageSize.width, (imageSize.height+ spacing),0.0);
CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: self.titleLabel.font}];
self.imageEdgeInsets = UIEdgeInsetsMake(
(titleSize.height+ spacing),0.0,0.0, - titleSize.width);
CGFloatedgeOffset =fabs(titleSize.height- imageSize.height) /2.0;
self.contentEdgeInsets=UIEdgeInsetsMake(edgeOffset,0.0, edgeOffset,0.0);
}
@end