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

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


.h文件

//

//  ZFSButton.h

//  ZFSNetWorkRequest

//

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

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

//


#import <UIKit/UIKit.h>

/********


 该自定的button是控制图片与文字未知的,有图片在上,文字在下,图片与文字左右适配等封装

 

*********/

typedef NS_ENUM(NSInteger, ZFSImageLocation) {

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

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

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

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

};


typedef NS_ENUM(NSInteger, ZFSOffSetDirection) {

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

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

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

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

};

@interface ZFSButton : UIButton


/**

 *  根据图片的位置和图片文字的间距来重新设置buttonimagetitle的排列

 *   如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮

 *

 *  @param location 图片位于文字的哪个方位

 *  @param spacing  图片和文字的间距离

 */

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


/**

 *  根据图片的位置和图片文字的间距来重新设置buttonimagetitle的排列,根据offset来确定整体要偏移的方向以及偏移的数值

 *   如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮

 *

 *  @param location         图片在文字的哪个方向

 *  @param spacing         图片和文字的间隔

 *  @param offSetDirection 哪个方向偏移

 *  @param offSetVar       偏移多少

 */

- (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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值