美文网首页iOS学习开发
iOS UIButton Image和title 的位置关系《转

iOS UIButton Image和title 的位置关系《转

作者: 酒红色T恤 | 来源:发表于2016-06-12 17:17 被阅读214次

    《转》http://www.cnblogs.com/Phelthas/p/4452235.html
    写一个UIButton的category

    使用方法:掉.h文件的唯一方法,传入你想要的图片位置枚举类型如:WZ_ImagePositionTop,即可。
    若有需要,传入文字和图片之间的间距spacing

    =============.h文件=========
    #import <UIKit/UIKit.h>

      typedef NS_ENUM(NSInteger, WZ_ImagePosition) {
    WZ_ImagePositionLeft = 0,              //图片在左,文字在右,默认
    WZ_ImagePositionRight = 1,             //图片在右,文字在左
    WZ_ImagePositionTop = 2,               //图片在上,文字在下
    WZ_ImagePositionBottom = 3,            //图片在下,文字在上
      };
    
      @interface UIButton (WZ_ImagePosition)
    
      /**
       *  利用UIButton的titleEdgeInsets和imageEdgeInsets来实现文字和图片的自由排列
       *  注意:这个方法需要在设置图片和文字之后才可以调用,且button的大小要大于 图片大小+文字大小+spacing
       *
       *  @param spacing 图片和文字的间隔
       */
      - (void)setImagePosition:(WZ_ImagePosition)postion spacing:(CGFloat)spacing;
    

    =============.m文件=========

     #import "UIButton+WZButtonImagePosition.h"
    
     @implementation UIButton (WZButtonImagePosition)
    - (void)setImagePosition:(WZ_ImagePosition)postion spacing:(CGFloat)spacing {
    [self setTitle:self.currentTitle forState:UIControlStateNormal];
    [self setImage:self.currentImage forState:UIControlStateNormal];
    
    
    CGFloat imageWidth = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-    Wdeprecated-declarations"
    CGFloat labelWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
    CGFloat labelHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;
    #pragma clang diagnostic pop
    
    CGFloat imageOffsetX = (imageWidth + labelWidth) / 2 - imageWidth / 2;//image中心移动的x距离
    CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;//image中心移动的y距离
    CGFloat labelOffsetX = (imageWidth + labelWidth / 2) - (imageWidth + labelWidth) / 2;//label中心移动的x距离
    CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;//label中心移动的y距离
    
    CGFloat tempWidth = MAX(labelWidth, imageWidth);
    CGFloat changedWidth = labelWidth + imageWidth - tempWidth;
    CGFloat tempHeight = MAX(labelHeight, imageHeight);
    CGFloat changedHeight = labelHeight + imageHeight + spacing - tempHeight;
    
    switch (postion) {
        case WZ_ImagePositionLeft:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
            
        case WZ_ImagePositionRight:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + spacing/2, 0, -(labelWidth + spacing/2));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth + spacing/2), 0, imageWidth + spacing/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
            
        case WZ_ImagePositionTop:
            self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, -changedWidth/2, changedHeight-imageOffsetY, -changedWidth/2);
            break;
            
        case WZ_ImagePositionBottom:
            self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(changedHeight-imageOffsetY, -changedWidth/2, imageOffsetY, -changedWidth/2);
            break;
            
        default:
            break;
    }
    
    }
    @end
    

    相关文章

      网友评论

        本文标题:iOS UIButton Image和title 的位置关系《转

        本文链接:https://www.haomeiwen.com/subject/pngedttx.html