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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章