实用小技巧(四):动态的增删标签视图

版本记录

版本号 时间
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

后续

还有很多小技巧和新鲜玩意给大家看,待续,哈哈~~~

星空
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容