iOS 標(biāo)簽Tag列表的實(shí)現(xiàn)代碼
前言
1、之前項(xiàng)目中會(huì)有一些標(biāo)簽列表來(lái)顯示某些特性或要求,如下圖(代碼實(shí)現(xiàn)后的效果):

2、期間也是瀏覽了好多其他的第三方,但是可能是沒(méi)找到好的方法去尋找吧,沒(méi)有找到一個(gè)合適的,況且又不是特別復(fù)雜的東西,所以就自己寫了一套,但是注意因?yàn)槲覀冺?xiàng)目中使用的是RAC+Mansory,所以想要使用的話需要引入這兩個(gè)庫(kù)=_=。
3、自己寫的時(shí)候考慮的不是太多,中心思想是ViewModel做定制需求,View通過(guò)ViewModel來(lái)實(shí)現(xiàn)定制化UI,其他更多的是邏輯上的排版,所以不做更多贅述,自己體會(huì)Y^o^Y 。
View布局
LSLabelTextView.h的實(shí)現(xiàn)
// // LSLabelTextView.h // RenCheRen // // Created by 王隆帥 on 15/12/30. // Copyright © 2015年 王隆帥. All rights reserved. // #import "YCView.h" @interface LSLabelTextView : YCView @end
LSLabelTextView.m的實(shí)現(xiàn)
//
// LSLabelTextView.m
// RenCheRen
//
// Created by 王隆帥 on 15/12/30.
// Copyright © 2015年 王隆帥. All rights reserved.
//
#import "LSLabelTextView.h"
#import "LSLabelTextViewModel.h"
@interface LSLabelTextView ()
@property (nonatomic, strong) LSLabelTextViewModel *viewModel;
@end
@implementation LSLabelTextView {
MASConstraint *_toLeftBtnMasConstraint;
MASConstraint *_toTopBtnMasonstraint;
MASConstraint *_toLeftSelfMasConstraint;
MASConstraint *_toTopSelfMasonstraint;
}
- (instancetype)initWithViewModel:(id<YCViewModelProtocol>)viewModel {
self.viewModel = (LSLabelTextViewModel *)viewModel;
return [super initWithViewModel:viewModel];
}
- (void)yc_bindViewModel {
@weakify(self);
[[RACObserve(self, viewModel.dataArray) distinctUntilChanged] subscribeNext:^(id x) {
@strongify(self);
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
if (self.viewModel.dataArray.count <= 0) {
__weak UIView *weakNullView;
UIView *nullView = [[UIView alloc] init];
weakNullView = nullView;
[self addSubview:weakNullView];
WS(weakSelf)
[weakNullView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.right.left.bottom.equalTo(weakSelf);
make.height.equalTo(weakSelf.viewModel.nullHeight);
}];
return;
}
NSInteger lineNum = 1;
CGFloat allWidth = 0;
__weak UIButton *lastBtn;
for (int i = 0; i < self.viewModel.dataArray.count; i++) {
NSString *string = [self.viewModel.dataArray stringWithIndex:i];
__weak UIButton *weakBtn = [self ls_getBtnWithString:string];
[self addSubview:weakBtn];
CGSize size = [string widthWithHeight:20 andFont:self.viewModel.textFontNum];
CGFloat needFloat = size.width < self.viewModel.miniWidth ? self.viewModel.miniWidth : size.width;
if (lastBtn) {
WS(weakSelf)
[weakBtn mas_makeConstraints:^(MASConstraintMaker *make) {
_toLeftBtnMasConstraint = make.left.equalTo(lastBtn.mas_right).offset(weakSelf.viewModel.labelHorizontalSpace);
[_toLeftBtnMasConstraint activate];
_toTopBtnMasonstraint = make.top.equalTo(lastBtn.mas_bottom).offset(weakSelf.viewModel.labelVerticalSpace);
[_toTopBtnMasonstraint deactivate];
_toLeftSelfMasConstraint = make.left.equalTo(weakSelf.viewModel.leftToViewEdge);
_toTopSelfMasonstraint = make.top.equalTo(lastBtn);
make.size.equalTo(CGSizeMake(needFloat + 20, weakSelf.viewModel.labelHeight));
}];
if (allWidth + self.viewModel.labelHorizontalSpace + needFloat + 20 + self.viewModel.rightToViewEdge > self.viewModel.allWidth) {
[_toLeftSelfMasConstraint activate];
[_toLeftBtnMasConstraint deactivate];
[_toTopBtnMasonstraint activate];
[_toTopSelfMasonstraint deactivate];
lineNum ++;
allWidth = self.viewModel.leftToViewEdge + needFloat + 20;
} else {
[_toLeftSelfMasConstraint deactivate];
[_toLeftBtnMasConstraint activate];
[_toTopBtnMasonstraint deactivate];
[_toTopSelfMasonstraint activate];
allWidth = allWidth + self.viewModel.labelHorizontalSpace + needFloat + 20;
}
} else {
WS(weakSelf)
[weakBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.viewModel.leftToViewEdge);
make.size.equalTo(CGSizeMake(needFloat + 20, weakSelf.viewModel.labelHeight));
make.top.equalTo(weakSelf.viewModel.topToViewEdge);
}];
allWidth = allWidth + self.viewModel.leftToViewEdge + needFloat + 20;
}
lastBtn = weakBtn;
}
WS(weakSlef)
[lastBtn mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(weakSlef.viewModel.bottomToViewEdge);
}];
}];
}
- (UIButton *)ls_getBtnWithString:(NSString *)string {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.layer.borderWidth = 0.5;
btn.layer.borderColor = self.viewModel.borderColor.CGColor;
[btn setTitleColor:self.viewModel.titleColor forState:UIControlStateNormal];
btn.backgroundColor = self.viewModel.backgroundColor;
btn.layer.masksToBounds = YES;
btn.layer.cornerRadius = self.viewModel.cornerRadius;
btn.titleLabel.font = YC_YAHEI_FONT(self.viewModel.textFontNum);
[btn setTitle:string forState:UIControlStateNormal];
btn.ls_typeString = string;
return btn;
}
@end
ViewModel適配
LSLabelTextViewModel.h的實(shí)現(xiàn)
// // LSLabelTextViewModel.h // RenCheRen // // Created by 王隆帥 on 15/12/30. // Copyright © 2015年 王隆帥. All rights reserved. // #import "YCViewModel.h" @interface LSLabelTextViewModel : YCViewModel /** * 標(biāo)簽數(shù)組 */ @property (nonatomic, strong) NSMutableArray *dataArray; /** * 總的寬度 */ @property (nonatomic, assign) CGFloat allWidth; /** * 沒(méi)有標(biāo)簽時(shí)的高度 */ @property (nonatomic, assign) CGFloat nullHeight; /** * 文字字體大小 */ @property (nonatomic, assign) CGFloat textFontNum; /** * 取得標(biāo)簽為空的時(shí)候,標(biāo)簽最小長(zhǎng)度 */ @property (nonatomic, assign) CGFloat miniWidth; /** * 標(biāo)簽高度 */ @property (nonatomic, assign) CGFloat labelHeight; /** * 最左側(cè)標(biāo)簽距離View的邊緣的寬度 */ @property (nonatomic, assign) CGFloat leftToViewEdge; /** * 最右側(cè)標(biāo)簽距離View的邊緣的寬度 */ @property (nonatomic, assign) CGFloat rightToViewEdge; /** * 最上側(cè)標(biāo)簽距離View的邊緣的寬度 */ @property (nonatomic, assign) CGFloat topToViewEdge; /** * 最下側(cè)標(biāo)簽距離View的邊緣的寬度 */ @property (nonatomic, assign) CGFloat bottomToViewEdge; /** * 橫向標(biāo)簽之間的寬度 */ @property (nonatomic, assign) CGFloat labelHorizontalSpace; /** * 縱向標(biāo)簽之間的寬度 */ @property (nonatomic, assign) CGFloat labelVerticalSpace; /** * label(btn) 的相關(guān)屬性 */ @property (nonatomic, assign) CGFloat borderWidth; @property (nonatomic, strong) UIColor *borderColor; @property (nonatomic, strong) UIColor *titleColor; @property (nonatomic, strong) UIColor *backgroundColor; @property (nonatomic, assign) CGFloat cornerRadius; @end
LSLabelTextViewModel.m的實(shí)現(xiàn)
//
// LSLabelTextViewModel.m
// RenCheRen
//
// Created by 王隆帥 on 15/12/30.
// Copyright © 2015年 王隆帥. All rights reserved.
//
#import "LSLabelTextViewModel.h"
@implementation LSLabelTextViewModel
- (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [[NSMutableArray alloc] init];
}
return _dataArray;
}
@end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- IOS實(shí)現(xiàn)展開二級(jí)列表效果
- IOS展開三級(jí)列表效果示例
- iOS多級(jí)列表實(shí)現(xiàn)代碼
- IOS實(shí)現(xiàn)簡(jiǎn)易版的QQ下拉列表
- iOS開發(fā)之在列表上方添加水印的方法
- ios基于UITableViewController實(shí)現(xiàn)列表
- iOS實(shí)現(xiàn)列表折疊效果
- iOS列表上拉(平滑加載數(shù)據(jù))自動(dòng)加載數(shù)據(jù)的問(wèn)題解決
- iOS實(shí)現(xiàn)從通訊錄中選擇聯(lián)系人
- iOS實(shí)現(xiàn)聯(lián)系人列表功能
相關(guān)文章
淺談iOS關(guān)于頭文件的導(dǎo)入問(wèn)題
本篇文章主要介紹了淺談iOS關(guān)于頭文件的導(dǎo)入問(wèn)題,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04
阿里數(shù)據(jù)iOS端啟動(dòng)速度優(yōu)化心得
本篇文章給大家詳細(xì)分析了阿里數(shù)據(jù)iOS端啟動(dòng)速度優(yōu)化的知識(shí)點(diǎn)以及心得,對(duì)此有興趣的朋友參考學(xué)習(xí)下吧。2018-02-02
IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView
這篇文章主要為大家介紹了IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
iOS App開發(fā)中的UISegmentedControl分段組件用法總結(jié)
UISegmentedControl主要被用來(lái)制作分頁(yè)按鈕或添加跳轉(zhuǎn)到不同位置的標(biāo)簽,這里我們就來(lái)看一下iOS App開發(fā)中的UISegmentedControl分段組件用法總結(jié),需要的朋友可以參考下2016-06-06
iOS動(dòng)畫-定時(shí)對(duì)UIView進(jìn)行翻轉(zhuǎn)和抖動(dòng)的方法
下面小編就為大家?guī)?lái)一篇iOS動(dòng)畫-定時(shí)對(duì)UIView進(jìn)行翻轉(zhuǎn)和抖動(dòng)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

