實用小技巧(四):動態的增刪標簽視圖

版本記錄

版本號 時間
V1.0 2017.06.02

前言

??很多app都有建立小組或者社區的功能,或者給某人添加幾個描述標簽等等,這些功能都需要動態的添加標簽視圖,這一篇就講述一下添加方法。感興趣的可以看看我寫的其他小技巧
1. 實用小技巧(一):UIScrollView中上下左右滾動方向的判斷

2. 實用小技巧(二):屏幕橫豎屏的判斷和相關邏輯
3.實用小技巧(三):點擊手勢屏蔽子視圖的響應

詳情

一、代碼組織結構

我們先看一下代碼組織結構圖。

代碼組織結構

二、代碼實現

1. JJTipVC.h

#import <UIKit/UIKit.h>

@interface JJTipVC : UIViewController

@end

2. JJTipVC.m

#import "JJTipVC.h"
#import "JJTipView.h"

@interface JJTipVC () <JJTipViewDelegate>

@property (nonatomic, strong) JJTipView *tipView;
@property (nonatomic, strong) NSMutableArray <NSString *>* tipArrM;

@end

@implementation JJTipVC

#pragma mark - Override Base Function

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupUI];
    
    self.tipArrM = [NSMutableArray array];
}

#pragma mark - Object Private Function

- (void)setupUI
{
    JJTipView *tipView = [[JJTipView alloc] initWithFrame:self.view.frame];
    tipView.delegate = self;
    [self.view addSubview:tipView];
    self.tipView = tipView;    
}

#pragma mark - JJTipViewDelegate

- (void)jjTipView:(JJTipView *)view tipContent:(NSString *)tipContentStr
{
    NSLog(@"控制器標簽按鈕");
    
    if (self.tipArrM.count == 5) {
        return;
    }
    
    tipContentStr = [tipContentStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    tipContentStr = [tipContentStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (tipContentStr.length > 0 && ![tipContentStr isEqualToString:@""]) {
        [self.tipArrM addObject:tipContentStr];
    }
    self.tipView.tipArr = self.tipArrM;
}

@end
3. JJTipView.h

#import <UIKit/UIKit.h>

@class JJTipView;

@protocol JJTipViewDelegate <NSObject>

//發送申請
- (void)jjTipView:(JJTipView *)view tipContent:(NSString *)tipContentStr;

@end

@interface JJTipView : UIView

@property (nonatomic, weak) id<JJTipViewDelegate>delegate;
@property (nonatomic, strong) NSMutableArray <NSString *>* tipArr;

@end

4. JJTipView.m
#import "JJTipView.h"
#import "Masonry.h"

@interface JJTipView ()

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UILabel *groupTipLabel;
@property (nonatomic, strong) UITextView *groupTipTextView;
@property (nonatomic, strong) UIButton *sendApplyButton;
@property (nonatomic, strong) NSMutableArray <UIButton *> *buttonArrM;
@property (nonatomic, strong) NSMutableArray <UIButton *> *deleteButtonArrM;

@end

@implementation JJTipView

#pragma mark - Override Base Function

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        
        self.buttonArrM = [NSMutableArray array];
        self.deleteButtonArrM = [NSMutableArray array];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //小組標簽
    [self.groupTipLabel sizeToFit];
    [self.groupTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topView).offset(10.0);
        make.top.equalTo(self.topView.mas_bottom).offset(40.0);
    }];
    
    //小組輸入框
    [self.groupTipTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.groupTipLabel);
        make.left.equalTo(self.groupTipLabel.mas_right).offset(20.0);
        make.width.equalTo(@250);
        make.height.equalTo(@40);
    }];
    
    //提交申請按鈕
    [self.sendApplyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.groupTipTextView.mas_bottom).offset(230.0);
        make.height.equalTo(@60);
    }];
}

#pragma mark - Object Private Function

- (void)setupUI
{
    self.backgroundColor = [UIColor lightTextColor];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 64.0, self.bounds.size.width, 100)];
    topView.backgroundColor = [UIColor blueColor];
    [self addSubview:topView];
    self.topView = topView;
    
    //小組標簽
    UILabel *groupTipLabel = [[UILabel alloc] init];
    groupTipLabel.text = @"小組標簽";
    groupTipLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:20.0];
    groupTipLabel.textColor = [UIColor blackColor];
    [self addSubview:groupTipLabel];
    self.groupTipLabel = groupTipLabel;
    
    //小組標簽輸入框
    UITextView *groupTipTextView = [[UITextView alloc] init];
    groupTipTextView.textColor = [UIColor blueColor];
    groupTipTextView.backgroundColor = [UIColor whiteColor];
    [self addSubview:groupTipTextView];
    self.groupTipTextView = groupTipTextView;
    
    //提交申請按鈕
    UIButton *sendApplyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sendApplyButton setTitle:@"添加標簽" forState:UIControlStateNormal];
    [sendApplyButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    sendApplyButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18.0];
    sendApplyButton.backgroundColor = [UIColor yellowColor];
    [sendApplyButton addTarget:self action:@selector(sendApplyButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    sendApplyButton.selected = YES;
    [self addSubview:sendApplyButton];
    self.sendApplyButton = sendApplyButton;
}

- (void)layoutGroupTip
{
    CGFloat width = 0.0;
    CGFloat totalWidth = 0.0;
    NSInteger lineNumber = 0;
    NSInteger number = 0;
    
    for (NSInteger i = 0; i < self.tipArr.count; i++) {
        //標簽按鈕
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        button.tag = 100 + i;
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 15.0;
        button.layer.borderColor = [UIColor yellowColor].CGColor;
        button.layer.borderWidth = 2;
        [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
        [button setTitle:self.tipArr[i] forState:UIControlStateNormal];
        [self.buttonArrM addObject:button];
        [self addSubview:button];
        
        CGSize titleSize = [self.tipArr[i] sizeWithFont:[UIFont fontWithName:@"PingFangSC-Regular" size:13.0] constrainedToSize:CGSizeMake(MAXFLOAT, 13.0)];
        totalWidth = totalWidth +titleSize.width + (number * 20);
        if (totalWidth > CGRectGetWidth(self.groupTipTextView.frame)) {
            totalWidth = 0.0;
            totalWidth = totalWidth + titleSize.width;
            lineNumber++;
            width = 0.0;
            width = width + titleSize.width;
            number = 1;
            button.frame = CGRectMake(CGRectGetMinX(self.groupTipTextView.frame), 20 + 40 * lineNumber + CGRectGetMaxY(self.groupTipTextView.frame), titleSize.width + 15.0, 30);
        }
        else {
            button.frame = CGRectMake(width + CGRectGetMinX(self.groupTipTextView.frame) + (number * 20), 20 + 40 * lineNumber + CGRectGetMaxY(self.groupTipTextView.frame), titleSize.width + 15.0, 30);
            width = width + titleSize.width;
            number++;
        }
        
        //刪除按鈕
        UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        deleteButton.tag = i;
        [self.deleteButtonArrM addObject:deleteButton];
        deleteButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        [deleteButton addTarget:self action:@selector(deleteTipButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [deleteButton setImage:[UIImage imageNamed:@"group_tip_close"] forState:UIControlStateNormal];
        [self addSubview:deleteButton];
        
        [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(button).offset(10.0);
            make.centerY.equalTo(button.mas_top);
            make.width.height.equalTo(@33);
        }];
    };
}

#pragma mark - Action && Notification

//添加標簽按鈕
- (void)sendApplyButtonDidClick:(UIButton *)button
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(jjTipView:tipContent:)]) {
        [self.delegate jjTipView:self tipContent:self.groupTipTextView.text];
    }
}

//刪除按鈕
- (void)deleteTipButtonDidClick:(UIButton *)button
{
    [self.tipArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%ld",button.tag);
        if (idx == button.tag) {
            [self.tipArr removeObjectAtIndex:idx];
            *stop = YES;
        }
    }];
    
    [self.buttonArrM enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.buttonArrM removeAllObjects];
    
    [self.deleteButtonArrM enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.deleteButtonArrM removeAllObjects];
    
    [self layoutGroupTip];
}

#pragma mark - Getter && Setter

- (void)setTipArr:(NSMutableArray<NSString *> *)tipArr
{
    _tipArr = tipArr;
    
    [self layoutGroupTip];
}

@end

三、實現結果

結果如下gif所示。

結果1
結果2
結果3
結果4

后續

還有很多小技巧和新鮮玩意給大家看,待續,哈哈~~~

星空
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容