天天看点

iOS UIButton文字和图片上下左右偏移封装,一个方法即可实现button上文字和图片不同位置的放置

        开发中,几乎都会需要时按钮上的文字在图片的上面,下面,左面,右面,然后就再次封装!  慢慢的,就自己试着写了一个封装,只需要调用一个方法就能实现文字和图片的不同位置展示!下面是代码:

.h文件

//

//  ZFSButton.h

//  ZFSNetWorkRequest

//

//  Created by HandsomeC on 16/12/12.

//  Copyright © 2016年 赵发生. All rights reserved.

//

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, ZFSImageLocation) {

    ZFSImageLocationLeft = 0,              //图片在文字的左边,默认

    ZFSImageLocationRight,             //图片在文字的右边

    ZFSImageLocationTop,               //图片在文字的上边

    ZFSImageLocationBottom,            //图片在文字的下边

};

typedef NS_ENUM(NSInteger, ZFSOffSetDirection) {

    ZFSOffSetDirectionLeft = 0,   //图片文字整体向左边偏移,默认

    ZFSOffSetDirectionRight,      //图片文字整体向右边偏移

    ZFSOffSetDirectionTop,        //图片文字整体向上边偏移

    ZFSOffSetDirectionBottom,     //图片文字整体向下边偏移

};

@interface ZFSButton : UIButton

- (void)setImageLocation:(ZFSImageLocation)location spacing:(CGFloat)spacing;

- (void)setImageLocation:(ZFSImageLocation)location spacing:(CGFloat)spacing offSet:(ZFSOffSetDirection)offSetDirection offSetVar:(CGFloat)offSetVar;

@end

.m文件

//

//  ZFSButton.m

//  ZFSNetWorkRequest

//

//  Created by HandsomeC on 16/12/12.

//  Copyright © 2016年 赵发生. All rights reserved.

//

#import "ZFSButton.h"

@implementation ZFSButton

- (void)setImageLocation:(ZFSImageLocation)location spacing:(CGFloat)spacing {

    CGFloat imageWith = self.imageView.image.size.width;

    CGFloat imageHeight = self.imageView.image.size.height;

    CGSize texth_wXQ = CGSizeMake(MAXFLOAT,MAXFLOAT);

    NSDictionary *dicText = @{NSFontAttributeName :self.titleLabel.font};

    CGFloat titleWidth = [self.titleLabel.text boundingRectWithSize:texth_wXQ options:NSStringDrawingUsesLineFragmentOrigin attributes:dicText context:nil].size.width;

    CGFloat titleHeight = [self.titleLabel.text boundingRectWithSize:texth_wXQ options:NSStringDrawingUsesLineFragmentOrigin attributes:dicText context:nil].size.height;

    //image中心移动的x距离

    CGFloat imageOffsetX = (imageWith + titleWidth) / 2 - imageWith / 2;

    //image中心移动的y距离

    CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;

    //title中心移动的x距离

    CGFloat titleOffsetX = (imageWith + titleWidth / 2) - (imageWith + titleWidth) / 2;

    //title中心移动的y距离

    CGFloat labelOffsetY = titleHeight / 2 + spacing / 2;

    switch (location) {

        case ZFSImageLocationLeft:

            self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);

            self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);

            break;

        case ZFSImageLocationRight:

            self.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth + spacing/2, 0, -(titleWidth + spacing/2));

            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageHeight + spacing/2), 0, imageHeight + spacing/2);

            break;

        case ZFSImageLocationTop:

            self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);

            self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -titleOffsetX, -labelOffsetY, titleOffsetX);

            break;

        case ZFSImageLocationBottom:

            self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);

            self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -titleOffsetX, labelOffsetY, titleOffsetX);

            break;

        default:

            break;

    }

}

- (void)setImageLocation:(ZFSImageLocation)location spacing:(CGFloat)spacing offSet:(ZFSOffSetDirection)offSetDirection offSetVar:(CGFloat)offSetVar{

    CGFloat imageWith = self.imageView.image.size.width;

    CGFloat imageHeight = self.imageView.image.size.height;

    CGSize texth_wXQ = CGSizeMake(MAXFLOAT,MAXFLOAT);

    NSDictionary *dicText = @{NSFontAttributeName :self.titleLabel.font};

    CGFloat titleWidth = [self.titleLabel.text boundingRectWithSize:texth_wXQ options:NSStringDrawingUsesLineFragmentOrigin attributes:dicText context:nil].size.width;

    CGFloat titleHeight = [self.titleLabel.text boundingRectWithSize:texth_wXQ options:NSStringDrawingUsesLineFragmentOrigin attributes:dicText context:nil].size.height;

    //image中心移动的x距离

    CGFloat imageOffsetX = (imageWith + titleWidth) / 2 - imageWith / 2;

    //image中心移动的y距离

    CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;

    //title中心移动的x距离

    CGFloat titleOffsetX = (imageWith + titleWidth / 2) - (imageWith + titleWidth) / 2;

    //title中心移动的y距离

    CGFloat labelOffsetY = titleHeight / 2 + spacing / 2;

    switch (location) {

        case ZFSImageLocationLeft:

            self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);

            self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);

            break;

        case ZFSImageLocationRight:

            self.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth + spacing/2, 0, -(titleWidth + spacing/2));

            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageHeight + spacing/2), 0, imageHeight + spacing/2);

            break;

        case ZFSImageLocationTop:

            self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);

            self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -titleOffsetX, -labelOffsetY, titleOffsetX);

            break;

        case ZFSImageLocationBottom:

            self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);

            self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -titleOffsetX, labelOffsetY, titleOffsetX);

            break;

        default:

            break;

    }

    CGFloat imageTop = self.imageEdgeInsets.top;

    CGFloat imageLeft = self.imageEdgeInsets.left;

    CGFloat imageBottom = self.imageEdgeInsets.bottom;

    CGFloat imageRight = self.imageEdgeInsets.right;

    CGFloat titleTop = self.titleEdgeInsets.top;

    CGFloat titleLeft = self.titleEdgeInsets.left;

    CGFloat titleBottom = self.titleEdgeInsets.bottom;

    CGFloat titleRight = self.titleEdgeInsets.right;

    switch (offSetDirection){

        case ZFSOffSetDirectionLeft:

            self.imageEdgeInsets = UIEdgeInsetsMake(imageTop, imageLeft - offSetVar, imageBottom, imageRight + offSetVar);

            self.titleEdgeInsets = UIEdgeInsetsMake(titleTop, titleLeft - offSetVar, titleBottom, titleRight + offSetVar);

            break;

        case ZFSOffSetDirectionRight:

            self.imageEdgeInsets = UIEdgeInsetsMake(imageTop, imageLeft + offSetVar, imageBottom, imageRight - offSetVar);

            self.titleEdgeInsets = UIEdgeInsetsMake(titleTop, titleLeft + offSetVar, titleBottom, titleRight - offSetVar);

            break;

        case ZFSOffSetDirectionTop:

            self.imageEdgeInsets = UIEdgeInsetsMake(imageTop - offSetVar , imageLeft, imageBottom + offSetVar, imageRight);

            self.titleEdgeInsets = UIEdgeInsetsMake(titleTop - offSetVar , titleLeft, titleBottom + offSetVar, titleRight);

            break;

        case ZFSOffSetDirectionBottom:

            self.imageEdgeInsets = UIEdgeInsetsMake(imageTop + offSetVar, imageLeft, imageBottom - offSetVar, imageRight);

            self.titleEdgeInsets = UIEdgeInsetsMake(titleTop + offSetVar, titleLeft, titleBottom - offSetVar, titleRight);

            break;

        default:

            break;

    }

}

@end