UIButton

作者: DDY | 来源:发表于2016-11-03 17:03 被阅读111次

    实现左文右图(默认)、左图右文、上图下文,上文下图

    Button.png

    一句话代码 [btn DDYStyle:DDYStyleImgRight padding:8];

    UIButton+DDYStyle.h

    //  Created by Rain Dou on 15/5/18.
    //  Copyright © 2015年 634778311 All rights reserved.
    
    #import <UIKit/UIKit.h>
    
    typedef NS_ENUM(NSInteger, DDYStyle) {
        DDYStyleDefault = 0,        // 左图右文,整体居中,默认状态
        DDYStyleImgLeft = 1,        // 左图右文,整体居中,设置间隙
        DDYStyleImgRight = 2,       // 左文右图,整体居中,设置间隙
        DDYStyleImgTop = 3,         // 上图下文,整体居中,设置间隙
        DDYStyleImgBottom = 4,      // 下图上文,整体居中,设置间隙
    };
    
    @interface UIButton (DDYStyle)
    
    /** style:图文样式 padding:图文间隙 */
    - (void)setDDYStyle:(DDYStyle)style padding:(CGFloat)padding;
    - (void)DDYStyle:(DDYStyle)style padding:(CGFloat)padding;
    
    @end
    

    UIButton+DDYStyle.m

    #import "UIButton+DDYStyle.h"
    
    @implementation UIButton (DDYStyle)
    
    - (void)setDDYStyle:(DDYStyle)style padding:(CGFloat)padding
    {
        if (self.imageView.image != nil && self.titleLabel.text != nil)
        {
            // 先还原
            self.titleEdgeInsets = UIEdgeInsetsZero;
            self.imageEdgeInsets = UIEdgeInsetsZero;
            
            // 上图下文或者上文下图情况重新计算高
            CGFloat imageX = self.imageView.ddy_x;
            CGFloat titleX = self.titleLabel.ddy_x;
            CGFloat imageY = self.imageView.ddy_y;
            CGFloat titleY = self.titleLabel.ddy_y;
            CGFloat imageW = self.imageView.ddy_w;
            CGFloat titleW = self.titleLabel.ddy_w;
            CGFloat imageH = self.imageView.ddy_h;
            CGFloat titleH = self.titleLabel.ddy_h;
            CGFloat totalH = imageH + titleH + padding;
            CGFloat selfH = self.ddy_h;
            CGFloat selfW = self.ddy_w;
            
            switch (style)
            {
                case DDYStyleImgLeft:
                    self.titleEdgeInsets = UIEdgeInsetsMake(0, padding/2.0, 0, -padding/2.0);
                    self.imageEdgeInsets = UIEdgeInsetsMake(0, -padding/2.0, 0, padding/2.0);
                    break;
                    
                case DDYStyleImgRight:
                    self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageW + padding/2.0), 0, (imageW + padding/2.0));
                    self.imageEdgeInsets = UIEdgeInsetsMake(0, (titleW + padding/2.0), 0, -(titleW + padding/2.0));
                    break;
                    
                case DDYStyleImgTop:
                    self.titleEdgeInsets = UIEdgeInsetsMake(((selfH - totalH)/2.0 + imageH + padding - titleY),
                                                            -(titleX + titleW/2.0 - selfW/2.0),
                                                            -((selfH - totalH)/2.0 + imageH + padding - titleY),
                                                            (titleX + titleW/2.0 - selfW/2.0));
                    self.imageEdgeInsets = UIEdgeInsetsMake(((selfH - totalH)/2.0 - imageY),
                                                            (selfW/2.0 - imageX - imageW/2.0),
                                                            -((selfH - totalH)/2.0 - imageY),
                                                            -(selfW/2.0 - imageX - imageW/2.0));
                    break;
                    
                case DDYStyleImgBottom:
                    NSLog(@"%f",titleX);
                    self.titleEdgeInsets = UIEdgeInsetsMake(((selfH - totalH)/2.0 - titleY),
                                                            -(titleX + titleW/2.0 - selfW/2.0),
                                                            -((selfH - totalH)/2.0 - titleY),
                                                            (titleX + titleW/2.0 - selfW/2.0));
                    self.imageEdgeInsets = UIEdgeInsetsMake(((selfH - totalH)/2.0 + titleH + padding - imageY),
                                                            (selfW/2.0 - imageX - imageW/2.0),
                                                            -((selfH - totalH)/2.0 + titleH + padding - imageY),
                                                            -(selfW/2.0 - imageX - imageW/2.0));
                    break;
                    
                default:
                    break;
            }
        }
    }
    - (void)DDYStyle:(DDYStyle)style padding:(CGFloat)padding {
        [self setDDYStyle:style padding:padding];
    }
    @end
    

    附:

    用到的viewCategory

    UIView+DDYExtension.h

    #import <UIKit/UIKit.h>
    
    @interface UIView (DDYExtension)
    
    @property (nonatomic, assign) CGFloat ddy_x;
    @property (nonatomic, assign) CGFloat ddy_y;
    @property (nonatomic, assign) CGFloat ddy_w;
    @property (nonatomic, assign) CGFloat ddy_h;
    @property (nonatomic, assign) CGFloat ddy_right;
    @property (nonatomic, assign) CGFloat ddy_bottom;
    @property (nonatomic, assign) CGFloat ddy_centerX;
    @property (nonatomic, assign) CGFloat ddy_centerY;
    @property (nonatomic, assign) CGSize  ddy_size;
    @property (nonatomic, assign) CGPoint ddy_origin;
    
    @end
    

    UIView+DDYExtension.m

    #import "UIView+DDYExtension.h"
    
    @implementation UIView (DDYExtension)
    
    /* x的setter和getter方法 */
    - (void)setDdy_x:(CGFloat)ddy_x {
        CGRect frame = self.frame;
        frame.origin.x = ddy_x;
        self.frame = frame;
    }
    - (CGFloat)ddy_x {
        return self.frame.origin.x;
    }
    
    /* y的setter和getter方法 */
    - (void)setDdy_y:(CGFloat)ddy_y {
        CGRect frame = self.frame;
        frame.origin.y = ddy_y;
        self.frame = frame;
    }
    - (CGFloat)ddy_y {
        return self.frame.origin.y;
    }
    
    /* width的setter和getter方法 */
    - (void)setDdy_w:(CGFloat)ddy_w {
        CGRect frame = self.frame;
        frame.size.width = ddy_w;
        self.frame = frame;
    }
    - (CGFloat)ddy_w {
        return self.frame.size.width;
    }
    
    /* height的setter和getter方法 */
    - (void)setDdy_h:(CGFloat)ddy_h {
        CGRect frame = self.frame;
        frame.size.height = ddy_h;
        self.frame = frame;
    }
    - (CGFloat)ddy_h {
        return self.frame.size.height;
    }
    /* centerX的setter和getter方法 */
    - (void)setDdy_centerX:(CGFloat)ddy_centerX {
        CGPoint center = self.center;
        center.x = ddy_centerX;
        self.center = center;
    }
    
    - (CGFloat)ddy_centerX {
        return self.center.x;
    }
    /* centerY的setter和getter方法 */
    - (void)setDdy_centerY:(CGFloat)ddy_centerY {
        CGPoint center = self.center;
        center.y = ddy_centerY;
        self.center = center;
    }
    
    - (CGFloat)ddy_centerY {
        return self.center.y;
    }
    
    /* 右边到 x 轴距离 */
    - (void)setDdy_right:(CGFloat)ddy_right {
        CGRect frame = self.frame;
        frame.origin.x = ddy_right - frame.size.width;
        self.frame = frame;
    }
    - (CGFloat)ddy_right {
        return self.frame.origin.x + self.frame.size.width;
    }
    /* 底边到 y 轴距离 */
    - (void)setDdy_bottom:(CGFloat)ddy_bottom {
        CGRect frame = self.frame;
        frame.origin.y = ddy_bottom - frame.size.height;
        self.frame = frame;
    }
    - (CGFloat)ddy_bottom {
        return self.frame.origin.y + self.frame.size.height;
    }
    
    /* size的setter和getter方法 */
    - (void)setDdy_size:(CGSize)ddy_size {
        CGRect frame = self.frame;
        frame.size = ddy_size;
        self.frame = frame;
    }
    - (CGSize)ddy_size {
        return self.frame.size;
    }
    
    /* origin的setter和getter方法 */
    - (void)setDdy_origin:(CGPoint)ddy_origin {
        CGRect frame = self.frame;
        frame.origin = ddy_origin;
        self.frame = frame;
    }
    - (CGPoint)ddy_origin {
        return self.frame.origin;
    }
    @end
    

    相关文章

      网友评论

          本文标题:UIButton

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