美文网首页其他一丢丢iOS 控件定制OC 开发分类(Category)
UIButton的分类——设置title相对于imageView

UIButton的分类——设置title相对于imageView

作者: 逍遥晨旭 | 来源:发表于2017-05-19 15:01 被阅读177次

    思路:

    1、获取UIButton的子控件imageView和titleLabel的size
    2、声明两个UIEdgeInsets变量imageEdgeInsets和labelEdgeInsets
    3、根据style(自定义的枚举)和space设置imageEdgeInsets和labelEdgeInsets的值
    4、最后将imageEdgeInsets和labelEdgeInsets的值赋值给UIButton的titleEdgeInsets 和imageEdgeInsets

    效果图如下:

    效果图
    typedef NS_ENUM(NSUInteger, CLButtonEdgeInsetsTitleStyle) {
        CLButtonEdgeInsetsStyleTitleTop, // title在上
        CLButtonEdgeInsetsStyleTitleLeft, // title在左
        CLButtonEdgeInsetsStyleTitleBottom, // title在下
        CLButtonEdgeInsetsStyleTitleRight // title在右
    };
    

    自定义的枚举样式

    /**
     设置button的titleLabel和imageView的布局样式,及间距
     
     @param style titleLabel和imageView的布局样式
     @param space titleLabel和imageView的间距
     */
    - (void)layoutButtonWithEdgeInsetsStyle:(CLButtonEdgeInsetsTitleStyle)style
                            imageTitleSpace:(CGFloat)space
    {
        // 1. 得到imageView和titleLabel的size
        CGSize titleSize = self.titleLabel.intrinsicContentSize;
        CGSize imageSize = self.imageView.bounds.size;
        
        // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
        UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
        UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
        
        // 3. 根据style和space设置imageEdgeInsets和labelEdgeInsets的值
        switch (style) {
            case CLButtonEdgeInsetsStyleTitleTop:
            {
                labelEdgeInsets = UIEdgeInsetsMake(0,-(imageSize.width), imageSize.height + space * 0.5, 0);
                imageEdgeInsets = UIEdgeInsetsMake(titleSize.height + space * 0.5, 0, 0, -(titleSize.width));
            }
                break;
            case CLButtonEdgeInsetsStyleTitleLeft:
            {
                imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width , 0, - titleSize.width - space * 0.5);
                labelEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, 0, imageSize.width + space * 0.5);
            }
                break;
            case CLButtonEdgeInsetsStyleTitleBottom:
            {
                imageEdgeInsets = UIEdgeInsetsMake(0,0, titleSize.height + space * 0.5, -(titleSize.width));
                labelEdgeInsets = UIEdgeInsetsMake(imageSize.height + space * 0.5, -(imageSize.width), 0, 0);
            }
                break;
            case CLButtonEdgeInsetsStyleTitleRight:
            {
                imageEdgeInsets = UIEdgeInsetsMake(0, self.imageEdgeInsets.left - space / 2, 0, 0);
                labelEdgeInsets = UIEdgeInsetsMake(0, self.titleEdgeInsets.left + space / 2, 0, 0);
            }
                break;
            default:
                break;
        }
        // 4. 赋值
        self.titleEdgeInsets = labelEdgeInsets;
        self.imageEdgeInsets = imageEdgeInsets;
    }
    

    注意:

    1、使用的时候要先设置Button的图片和title,然后在调用- (void)layoutButtonWithEdgeInsetsStyle:(CLButtonEdgeInsetsTitleStyle)style
    imageTitleSpace:(CGFloat)space;方法。

    2、获取UIButton的子控件imageView和titleLabel的size所用方法不一样。具体原因不是很清楚。

    相关文章

      网友评论

      • iOS白水:具体原因是 iOS8 获取 titlelabel 会为0

      本文标题:UIButton的分类——设置title相对于imageView

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