• 自定义控件(1)- 标签页


    功能:

    1,使用Masonry实现的标签页,内部自动根据内容提高自身约束高度

    2,有三种模式(多选,单选,和点击),标签本身能够携带数据,能够设置默认选中值,能够拿到已选中标签的数据,能够显示图片


    注意:

    1,选中和未选中图片需要自己进入.m设置

    2,必须设置最大宽度,否者布局会错

    3, 内部使用的button是自定义能够携带数据的BZButton(就是给button加了一个id类型的data属性,用来携带数据)


    源码地址:

    https://github.com/SSBun/iOS-Custom-Control/tree/master/自适应标签页(1)


     

    //  BZFallsSelectView.h
    //  668PetHotel
    //
    //  Created by 蔡士林 on 15/12/10.
    //  Copyright © 2015年 wby. All rights reserved.
    //  需要使用Masonry
    
    // BZFallsSelectView.h
    
    typedef enum : NSUInteger {
        BZFallsSelectViewTypeMultiSelect, // default 多选标签
        BZFallsSelectViewTypeRadioSelect, // 单选标签
        BZFallsSelectViewTypeTag,         // 点击标签
    } BZFallsSelectViewType;
    
    #import <UIKit/UIKit.h>
    /// 标签的数据源协议
    @interface BZFallsSelectViewModel : NSObject
    /// 标签标题
    @property (copy, nonatomic) NSString *title;
    /// 标签携带的数据
    @property (strong, nonatomic) id data;
    /// 标签的图片
    @property (strong, nonatomic) UIImage *iconImage;
    @end
    
    @class BZFallsSelectView;
    @protocol BZFallsSelectViewDelegate <NSObject>
    /**
     *    @brief  点击标签时触发
     */
    - (void)fallsSelectView:(BZFallsSelectView *)fallsSelectView didSelected:(BZFallsSelectViewModel *)selectedModel;
    
    @end
    
    @interface BZFallsSelectView : UIView
    /// 所有的选项数据
    @property (strong, nonatomic) NSArray<BZFallsSelectViewModel *> *datas;
    /// 默认选中的值
    @property (strong, nonatomic) NSArray<BZFallsSelectViewModel *> *defaultDatas;
    /// 最大宽度(使用masonry自动布局,所以需要知道)
    @property (assign, nonatomic) CGFloat maxWidth;
    /// 选中后的值
    @property (strong, nonatomic) NSMutableArray<BZFallsSelectViewModel *> *selectedDatas;
    /// 标签页类型
    @property (assign, nonatomic) BZFallsSelectViewType fallsSelectViewType;
    /// 代理协议
    @property (weak, nonatomic) id <BZFallsSelectViewDelegate> delegate;
    /// 布局完成后当前视图的高度
    @property (assign, nonatomic) CGFloat currentHeight;
    @end
    //
    //  BZFallsSelectView.m
    //  668PetHotel
    //
    //  Created by 蔡士林 on 15/12/10.
    //  Copyright © 2015年 wby. All rights reserved.
    //
    
    // BZFallsSelectView.m
    
    #define kMargin  15
    #define kLeading 10
    #define kBtnH    25
    #import "BZFallsSelectView.h"
    #import "BZButton.h"
    
    @implementation BZFallsSelectViewModel
    
    @end
    
    @interface BZFallsSelectView ()
    @property (strong, nonatomic) BZButton *allBtn;
    @property (strong, nonatomic) NSMutableArray *btnArr;
    
    @property (strong, nonatomic) UIButton *lastBtn;
    @end
    
    @implementation BZFallsSelectView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.defaultDatas = [NSArray array];
            self.fallsSelectViewType = BZFallsSelectViewTypeMultiSelect;
            [self setUpUI];
        }
        return self;
    }
    
    - (void)setUpUI
    {
        BZButton *allBtn = [self addSelectBtn:@"全选" data:nil];
        self.allBtn = allBtn;
    }
    
    - (BZButton *)addSelectBtn:(NSString *)title data:(BZFallsSelectViewModel *)data
    {
        BZButton *btn = [[BZButton alloc]init];
        btn.data = data;
        btn.titleLabel.font = [UIFont systemFontOfSize:11];
        [btn setBackgroundImage:[UIImage resizeImageWithName:@"shezhi_xuanze_da_anxia"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage resizeImageWithName:@"shezhi_xuanze_da_zhengchang"] forState:UIControlStateSelected];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setImage:data.iconImage forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn  setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        return btn;
    }
    
    - (void)btnDidClick:(BZButton *)sender
    {
        if (self.fallsSelectViewType == BZFallsSelectViewTypeTag) {
            if ([self.delegate respondsToSelector:@selector(fallsSelectView:didSelected:)]) {
                [self.delegate fallsSelectView:self didSelected:sender.data];
            }
        }
        else if (self.fallsSelectViewType == BZFallsSelectViewTypeRadioSelect)
        {
            self.lastBtn.selected = NO;
            sender.selected = YES;
            self.lastBtn = sender;
        }
        else
        {
            sender.selected = !sender.isSelected;
            if ([sender.titleLabel.text isEqualToString:@"全选"]) {
                for (BZButton *btn in self.btnArr) {
                    btn.selected = sender.isSelected;
                }
            }
        }
    }
    
    - (CGSize)sizeOfBtnWithString:(NSString *)title
    {
        CGSize size = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, kBtnH) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11]} context:nil].size;
        size = CGSizeMake(size.width + 20, kBtnH);
        return size;
    }
    
    - (void)setDatas:(NSArray<BZFallsSelectViewModel *> *)datas
    {
        _datas = datas;
        for (UIButton *btn in self.btnArr) {
            [btn removeFromSuperview];
        }
        self.btnArr = nil;
        self.btnArr = [NSMutableArray array];
        for (BZFallsSelectViewModel *model in datas) {
            BZButton *btn = [self addSelectBtn:model.title data:model];
            for (BZFallsSelectViewModel *defaultModel in self.defaultDatas) {
                if ([model.title isEqualToString:defaultModel.title]) {
                    btn.selected = YES;
                    continue;
                }
            }
            [self.btnArr addObject:btn];
        }
        [self setUpLayout];
    }
    
    - (void)setDefaultDatas:(NSArray *)defaultDatas
    {
        _defaultDatas = defaultDatas;
        if (self.btnArr.count > 0) {
            for (BZButton *btn in self.btnArr) {
                BZFallsSelectViewModel *btnModel = (BZFallsSelectViewModel *)btn.data;
                for (BZFallsSelectViewModel *defaultModel in self.defaultDatas) {
                    if ([btnModel.title isEqualToString:defaultModel.title]) {
                        btn.selected = YES;
                        continue;
                    }
                }
            }
        }
    }
    
    - (void)setUpLayout
    {
        if (self.fallsSelectViewType == BZFallsSelectViewTypeMultiSelect) {
            self.allBtn.frame = CGRectMake(kMargin, 0, [self sizeOfBtnWithString:@"全部"].width,kBtnH);
        }
        else
        {
            self.allBtn.frame = CGRectMake(0, 0, 0, 0);
        }
        NSInteger currRow = 0;
        BZButton *lastBtn = self.allBtn;
        
        for (BZButton *btn in self.btnArr) {
            BZFallsSelectViewModel *btnModel = (BZFallsSelectViewModel *)btn.data;
            btn.width = [self sizeOfBtnWithString:btnModel.title].width;
            btn.height = kBtnH;
            btn.y = (kBtnH + kLeading) * currRow;
            CGFloat btnEndX = CGRectGetMaxX(lastBtn.frame) + kMargin + btn.width;
            if (btnEndX > self.maxWidth) {
                btn.x = kMargin;
                currRow ++;
                btn.y = (kBtnH + kLeading) * currRow;
            }
            else
            {
                btn.x = CGRectGetMaxX(lastBtn.frame) + kMargin;
            }
            lastBtn = btn;
        }
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(CGRectGetMaxY(lastBtn.frame));
        }];
        self.height = CGRectGetMaxY(lastBtn.frame);
    }
    
    - (NSMutableArray *)selectedDatas
    {
        NSMutableArray *tempArr = [NSMutableArray array];
        for (BZButton *btn  in self.btnArr) {
            if (btn.isSelected) {
                [tempArr addObject:btn.data];
            }
        }
        return tempArr;
    }
    
    @end

     

  • 相关阅读:
    实战演练:通过伪列、虚拟列实现SQL优化
    python try else
    Prince2是怎么考试的?
    Prince2是怎么考试的?
    Prince2是怎么考试的?
    Prince2是怎么考试的?
    拦截器
    拦截器
    拦截器
    拦截器
  • 原文地址:https://www.cnblogs.com/SSBun/p/5142021.html
Copyright © 2020-2023  润新知