国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS 標簽Tag列表的實現代碼

2020-07-26 02:51:14
字體:
來源:轉載
供稿:網友

前言

1、之前項目中會有一些標簽列表來顯示某些特性或要求,如下圖(代碼實現后的效果):

2、期間也是瀏覽了好多其他的第三方,但是可能是沒找到好的方法去尋找吧,沒有找到一個合適的,況且又不是特別復雜的東西,所以就自己寫了一套,但是注意因為我們項目中使用的是RAC+Mansory,所以想要使用的話需要引入這兩個庫=_=。

3、自己寫的時候考慮的不是太多,中心思想是ViewModel做定制需求,View通過ViewModel來實現定制化UI,其他更多的是邏輯上的排版,所以不做更多贅述,自己體會Y^o^Y 。

View布局

LSLabelTextView.h的實現

//// LSLabelTextView.h// RenCheRen//// Created by 王隆帥 on 15/12/30.// Copyright © 2015年 王隆帥. All rights reserved.//#import "YCView.h"@interface LSLabelTextView : YCView@end

LSLabelTextView.m的實現

//// 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的實現

//// LSLabelTextViewModel.h// RenCheRen//// Created by 王隆帥 on 15/12/30.// Copyright © 2015年 王隆帥. All rights reserved.//#import "YCViewModel.h"@interface LSLabelTextViewModel : YCViewModel/** * 標簽數組 */@property (nonatomic, strong) NSMutableArray *dataArray;/** * 總的寬度 */@property (nonatomic, assign) CGFloat allWidth;/** * 沒有標簽時的高度 */@property (nonatomic, assign) CGFloat nullHeight;/** * 文字字體大小 */@property (nonatomic, assign) CGFloat textFontNum;/** * 取得標簽為空的時候,標簽最小長度 */@property (nonatomic, assign) CGFloat miniWidth;/** * 標簽高度 */@property (nonatomic, assign) CGFloat labelHeight;/** * 最左側標簽距離View的邊緣的寬度 */@property (nonatomic, assign) CGFloat leftToViewEdge;/** * 最右側標簽距離View的邊緣的寬度 */@property (nonatomic, assign) CGFloat rightToViewEdge;/** * 最上側標簽距離View的邊緣的寬度 */@property (nonatomic, assign) CGFloat topToViewEdge;/** * 最下側標簽距離View的邊緣的寬度 */@property (nonatomic, assign) CGFloat bottomToViewEdge;/** * 橫向標簽之間的寬度 */@property (nonatomic, assign) CGFloat labelHorizontalSpace;/** * 縱向標簽之間的寬度 */@property (nonatomic, assign) CGFloat labelVerticalSpace;/** * label(btn) 的相關屬性 */@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的實現

//// 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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青龙| 荆门市| 阳泉市| 察哈| 青铜峡市| 法库县| 那坡县| 微山县| 永嘉县| 通化县| 恩施市| 台前县| 景宁| 浠水县| 晋宁县| 勃利县| 罗平县| 永丰县| 奈曼旗| 吴旗县| 定日县| 伊川县| 东乡县| 赤峰市| 马龙县| 利川市| 军事| 南溪县| 南投市| 南阳市| 平昌县| 北海市| 同心县| 清苑县| 黑河市| 定边县| 修武县| 柳林县| 通渭县| 盈江县| 德惠市|