版本记录
版本号 | 时间 |
---|---|
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所示。
后续
还有很多小技巧和新鲜玩意给大家看,待续,哈哈~~~