美文网首页
Button 上文字及图片位置设置

Button 上文字及图片位置设置

作者: 杂货铺学徒 | 来源:发表于2019-08-01 14:45 被阅读0次

    .h

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

    .m

    #import "UIButton+ImagePosition.h"
    
    @implementation UIButton (ImagePosition)
    
    - (void)setImagePosition:(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 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 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 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 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
    

    相关文章

      网友评论

          本文标题:Button 上文字及图片位置设置

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