VideoCateVC.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // VideoCateVC.m
  3. // BuguLive
  4. //
  5. // Created by bugu on 2019/12/2.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "VideoCateVC.h"
  9. #import "DTTopicModel.h"
  10. @interface VideoCateVC ()
  11. @property (nonatomic,strong) NSMutableArray *dataArray;
  12. @end
  13. @implementation VideoCateVC
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. self.dataArray = [NSMutableArray array];
  17. self.title = ASLocalizedString(@"分类");
  18. [self backBtnWithBlock];
  19. self.dataArray = [NSMutableArray arrayWithArray:[GlobalVariables sharedInstance].appModel.video_cate];
  20. [self createLabelUI];
  21. // [self requestData];
  22. // Do any additional setup after loading the view.
  23. }
  24. - (void)backBtnWithBlock
  25. {
  26. // 返回按钮
  27. [self setupBackBtnWithBlock:nil];
  28. }
  29. - (void)requestData{
  30. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  31. [parmDict setObject:@"dynamic" forKey:@"ctl"];
  32. [parmDict setObject:@"dynamic_cate" forKey:@"act"];
  33. FWWeakify(self)
  34. [[NetHttpsManager manager] POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  35. NSArray * array = responseJson[@"data"];
  36. for (id obj in array)
  37. {
  38. DTTopicModel *model =[DTTopicModel mj_objectWithKeyValues:obj];
  39. [self.dataArray addObject:model];
  40. }
  41. // [self.tableView reloadData];
  42. [self createLabelUI];
  43. } FailureBlock:^(NSError *error) {
  44. }];
  45. }
  46. //创建标签流UI
  47. -(void)createLabelUI{
  48. #pragma mark ============== 标签流布局 S====================
  49. //父视图
  50. UIView * labelContentView = [[UIView alloc]init];
  51. [self.view addSubview:labelContentView];
  52. //标签相对父视图左边距
  53. CGFloat leftMarginLabel = 10;
  54. //标签相对父视图顶部距离
  55. CGFloat topMarginLabel = 0;
  56. //标签左右间距
  57. CGFloat horizontalSpace = 10;
  58. //标签上下间距
  59. CGFloat verticalSpace = 10;
  60. //标签的高度
  61. CGFloat labelHeight = 55;
  62. //下面这几个值不需要做修改
  63. //最大宽度,超过这个宽度就要换行
  64. CGFloat windthMax = SCREEN_WIDTH - leftMarginLabel * 2;
  65. //标签的起始X坐标(下面动态变化)
  66. CGFloat labelMinX = leftMarginLabel;
  67. //标签的起始Y坐标(下面动态变化)
  68. CGFloat labelMimY = topMarginLabel;
  69. //记录最后一个标签的最大Y,用来布局父视图
  70. CGFloat lastLabelMaxY = topMarginLabel;
  71. for (int i = 0; i < self.dataArray.count; i++) {
  72. DTTopicModel * model = self.dataArray[i];
  73. NSString * title = model.name;
  74. QMUIButton* labelButton = [QMUIButton buttonWithType:UIButtonTypeCustom];
  75. [labelButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  76. [labelButton setImage:[UIImage imageNamed:@"添加短视频分类"] forState:UIControlStateNormal];
  77. labelButton.imagePosition = QMUIButtonImagePositionLeft;
  78. labelButton.spacingBetweenImageAndTitle = 4;
  79. // [labelButton setTitleColor:MAIN_COLOR forState:UIControlStateSelected];
  80. //根据文字计算标签的宽度,后面会多加上一点宽度,视情况而定
  81. NSDictionary * attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
  82. CGFloat labelWidth = [title boundingRectWithSize:CGSizeMake(FLT_MAX, FLT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width + 30;
  83. //为标签赋值
  84. [labelButton setTitle:[NSString stringWithFormat:@"%@",title] forState:UIControlStateNormal];
  85. //设置标签的frame
  86. labelButton.frame = CGRectMake(labelMinX, labelMimY, labelWidth, labelHeight);
  87. //当标签的位置超出屏幕边缘时换行(超出限制的最大宽度)
  88. if(labelMinX + labelWidth + horizontalSpace > windthMax){
  89. //换行时将w置为起始坐标
  90. labelMinX = leftMarginLabel;
  91. //距离父视图也变化
  92. labelMimY = labelMimY + labelButton.frame.size.height + verticalSpace;
  93. //重设button的frame
  94. labelButton.frame = CGRectMake(labelMinX, labelMimY, labelWidth, labelHeight);
  95. }
  96. //多加的是两个标签之间的水平距离
  97. labelMinX = labelButton.frame.size.width + labelButton.frame.origin.x + horizontalSpace;
  98. labelButton.titleLabel.font = [UIFont systemFontOfSize:16];
  99. labelButton.tag = i+100;
  100. //标签的点击事件
  101. [labelButton addTarget:self action:@selector(labelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  102. [labelContentView addSubview:labelButton];
  103. if (i==self.dataArray.count-1) {
  104. //拿到最后一个标签的位置
  105. lastLabelMaxY = labelButton.bottom;
  106. }
  107. labelButton.backgroundColor = kWhiteColor;
  108. labelButton.layer.cornerRadius = 3;
  109. labelButton.layer.shadowColor = [UIColor colorWithRed:192/255.0 green:192/255.0 blue:192/255.0 alpha:0.5].CGColor;
  110. labelButton.layer.shadowOffset = CGSizeMake(0,0);
  111. labelButton.layer.shadowOpacity = 1;
  112. labelButton.layer.shadowRadius = 5;
  113. }
  114. //布局
  115. [labelContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  116. make.top.mas_equalTo(10);
  117. make.left.right.mas_equalTo(0);
  118. make.height.mas_equalTo(lastLabelMaxY);
  119. }];
  120. #pragma mark ============== 标签流布局 E====================
  121. }
  122. -(void)labelButtonAction:(UIButton *)button{
  123. NSInteger index = button.tag -100;
  124. DTTopicModel * model = self.dataArray[index];
  125. !self.releaseTopicBlock?:self.releaseTopicBlock(model);
  126. [self.navigationController popViewControllerAnimated:YES];
  127. }
  128. /*
  129. #pragma mark - Navigation
  130. // In a storyboard-based application, you will often want to do a little preparation before navigation
  131. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  132. // Get the new view controller using [segue destinationViewController].
  133. // Pass the selected object to the new view controller.
  134. }
  135. */
  136. @end