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

来源:互联网 发布:java 获取前一天日期 编辑:程序博客网 时间:2024/05/22 12:19

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


.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.textboundingRectWithSize:texth_wXQoptions:NSStringDrawingUsesLineFragmentOriginattributes:dicText context:nil].size.width;


    CGFloat titleHeight = [self.titleLabel.textboundingRectWithSize:texth_wXQoptions:NSStringDrawingUsesLineFragmentOriginattributes: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) {

        caseZFSImageLocationLeft:

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

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

            break;

            

        caseZFSImageLocationRight:

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

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

            break;

            

        caseZFSImageLocationTop:

            

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

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

            break;

            

        caseZFSImageLocationBottom:

            

            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.textboundingRectWithSize:texth_wXQoptions:NSStringDrawingUsesLineFragmentOriginattributes:dicText context:nil].size.width;

    

    CGFloat titleHeight = [self.titleLabel.textboundingRectWithSize:texth_wXQoptions:NSStringDrawingUsesLineFragmentOriginattributes: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) {

        caseZFSImageLocationLeft:

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

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

            break;

            

        caseZFSImageLocationRight:

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

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

            break;

            

        caseZFSImageLocationTop:

            

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

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

            break;

            

        caseZFSImageLocationBottom:

            

            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){

        caseZFSOffSetDirectionLeft:

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

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

            

            break;

        caseZFSOffSetDirectionRight:

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

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

            break;

        caseZFSOffSetDirectionTop:

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

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

            break;

        caseZFSOffSetDirectionBottom:

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

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

            break;

        default:

            break;

    }

    

}



@end


阅读全文
0 0