iOS 设置UIButton image、label位置

iOS 设置UIButton image、label位置_第1张图片
![3ED327FA-1DD1-4DF7-B7BF-ED1F81D41EC4.png](http://upload-images.jianshu.io/upload_images/2795270-73b98035fe82d12c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

iOS 设置UIButton image、label位置_第2张图片
9881D3CE-2626-448B-B968-9B928ACBBD42.png

iOS 设置UIButton image、label位置_第3张图片
858008E6-E0CB-4C63-801C-F822CD253526.png

button共有8种类型,这是其中的4种是居中的,其它的居左、居右就不展示了。这个按钮你只用设置image、title、title的font以及button的类型就可以了,你不必去关注imageEdgeInsets、titleEdgeInsets的设置(这个东西自己设置的话是很纠结的),这些我都做好了,用起来还是蛮方便的。但是必须注意以下两点:
1、按钮必须容得下image+titleLabel
2、Image、Title 、titleFontValue、它们之间间隔都必须在设置locationType(按钮类型)之前设置,因为这里要计算frame进行调整

下面就上代码了:



#import 

typedef NS_ENUM(NSInteger,ImageTitleLocationType) {
    ImageLocateLeftAndTotalCenter=0,
    ImageLocateRightAndTotalCenter,
    ImageLocateLeftAndTotalLeft,
    ImageLocateRightAndTotalLeft,
    ImageLocateLeftAndTotalRight,
    ImageLocateRightAndTotalRight,
    ImageLocateTopAndTotalCenter,
    ImageLocateBottomAndTotalCenter,
};

@interface SAButton : UIButton

@property(nonatomic,assign) ImageTitleLocationType locationType;

@property(nonatomic,assign) float margin;


@end


#import "SAButton.h"

@interface SAButton(){
    float imageWidth;
    float imageHeight;
    float titleWidth;
    float titleheight;
}

@end

@implementation SAButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _titleFontValue=17.0;
        _interval = 0.0;
    }
    return self;
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state{
    [super setTitle:title forState:state];
    CGRect rect = [self getRectByString:title];
    /*
     为什么要多加6  经测算在Font17.0大小下  系统算的titleLabel.size.width比我们算的多12个像素
     根据Font的不同 酌情改变 想精确的话就要自己去测算了
     */
    titleWidth = rect.size.width+6;
    titleheight = rect.size.height;
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state{
    [super setImage:image forState:state];
    imageWidth = image.size.width;
    imageHeight = image.size.height;
}

- (void)setInterval:(float)interval{
    _interval = interval;
}

- (void)setTitleFont:(float)titleFontValue{
    _titleFontValue = titleFontValue;
}

- (CGRect)getRectByString:(NSString*)string{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:_titleFontValue]} context:nil];
    return rect;
}

- (void)setLocationType:(ImageTitleLocationType)locationType{
    switch (locationType) {
        case 0:
        {
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -_interval*0.5, 0, _interval*0.5);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, _interval*0.5, 0,-_interval*0.5);
        }
            break;
        case 1:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,titleWidth+_interval*0.5, 0, -(titleWidth+_interval*0.5));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth+_interval*0.5), 0, imageWidth+_interval*0.5);
        }
            break;
        case 2:
        {
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, _interval, 0,-_interval);

        }
            break;
        case 3:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,titleWidth+_interval, 0, -(titleWidth+_interval));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        }
            break;
        case 4:
        {
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -_interval, 0, _interval);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0,0);
        }
            break;
        case 5:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,titleWidth, 0, -(titleWidth));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth+_interval), 0, imageWidth+_interval);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        }
            break;
        case 6:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,0, titleheight + _interval, -(titleWidth));
            self.titleEdgeInsets = UIEdgeInsetsMake(imageHeight + _interval, -(imageWidth), 0, 0);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        }
            break;
        case 7:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,0, -(titleheight + _interval), -titleWidth);
            self.titleEdgeInsets = UIEdgeInsetsMake(-(imageHeight + _interval),-imageWidth, 0, 0);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        }
            break;
        default:
            break;
    }
}

@end

你可能感兴趣的:(iOS 设置UIButton image、label位置)