美文网首页
OC: UIButton+MaxMethod

OC: UIButton+MaxMethod

作者: 一欧Yiou | 来源:发表于2018-12-13 10:29 被阅读0次

    .h文件

    // 定义一个枚举(包含了四种类型的button)
    typedef NS_ENUM(NSInteger, ButtonImageEdgeInsetsStyle) {
        /**
         *  image在上,label在下
         */
        ButtonImageEdgeInsetsStyleTop,
        /**
         *  image在左,label在右
         */
        ButtonImageEdgeInsetsStyleLeft,
        /**
         *  image在下,label在上
         */
        ButtonImageEdgeInsetsStyleBottom,
        /**
         *  image在右,label在左
         */
        ButtonImageEdgeInsetsStyleRight
    };
    
    #import <UIKit/UIKit.h>
    
    @interface UIButton (MaxMethod)
    
    /**
     * 设置button的titleLabel和imageView的布局样式,及间距
     *
     * @param style titleLabel和imageView的布局样式
     * @param space titleLabel和imageView的间距
     */
    - (void)layoutButtonWithEdgeInsetsStyle:(ButtonImageEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space;
    
    
    /**
     设置角标的个数(右上角)
     @param badgeValue <#badgeValue description#>
     */
    - (void)setBadgeValue:(NSInteger)badgeValue;
    
    @end
    

    .m文件

    #import "UIButton+MaxMethod.h"
    
    @implementation UIButton (MaxMethod)
    
    - (void)layoutButtonWithEdgeInsetsStyle:(ButtonImageEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space {
        // 1. 得到imageView和titleLabel的宽、高
        CGFloat imageWith = self.imageView.frame.size.width;
        CGFloat imageHeight = self.imageView.frame.size.height;
        
        CGFloat labelWidth = 0.0;
        CGFloat labelHeight = 0.0;
        
        if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
            // 由于iOS8中titleLabel的size为0,用下面的这种设置
            labelWidth = self.titleLabel.intrinsicContentSize.width;
            labelHeight = self.titleLabel.intrinsicContentSize.height;
        } else {
            labelWidth = self.titleLabel.frame.size.width;
            labelHeight = self.titleLabel.frame.size.height;
        }
        
        // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
        UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
        UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
        
        // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
        
        switch (style) {
            case ButtonImageEdgeInsetsStyleTop:
            {
                imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
                labelEdgeInsets = UIEdgeInsetsMake(0, -14, -imageHeight-space/2.0, 0);
            }
                break;
                
            case ButtonImageEdgeInsetsStyleLeft:
            {
                imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
                labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
            }
                break;
                
            case ButtonImageEdgeInsetsStyleBottom:
            {
                imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
                labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
            }
                break;
                
            case ButtonImageEdgeInsetsStyleRight:
            {
                imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
                labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
            }
                break;
                
            default:
                break;
        }
        
        // 4. 赋值
        self.titleEdgeInsets = labelEdgeInsets;
        self.imageEdgeInsets = imageEdgeInsets;
    }
    
    
    /**
     设置角标的个数(右上角)
     
     @param badgeValue <#badgeValue description#>
     */
    - (void)setBadgeValue:(NSInteger)badgeValue{
        
        CGFloat badgeW   = 20;
        CGSize imageSize = self.imageView.frame.size;
        CGFloat imageX   = self.imageView.frame.origin.x;
        CGFloat imageY   = self.imageView.frame.origin.y;
        
        UILabel *badgeLable = [[UILabel alloc]init];
        badgeLable.text = [NSString stringWithFormat:@"%ld",badgeValue];
        badgeLable.textAlignment = NSTextAlignmentCenter;
        badgeLable.textColor = [UIColor whiteColor];
        badgeLable.font = [UIFont systemFontOfSize:12];
        badgeLable.layer.cornerRadius = badgeW*0.5;
        badgeLable.clipsToBounds = YES;
        badgeLable.backgroundColor = [UIColor redColor];
        
        CGFloat badgeX = imageX + imageSize.width - badgeW*0.5;
        CGFloat badgeY = imageY - badgeW*0.25;
        badgeLable.frame = CGRectMake(badgeX, badgeY, badgeW, badgeW);
        [self addSubview:badgeLable];
    }
    @end
    

    相关文章

      网友评论

          本文标题:OC: UIButton+MaxMethod

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