GroupSynopsisCell.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // GroupSynopsisCell.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/6/15.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "GroupSynopsisCell.h"
  9. #import "SynopsisCollectCell.h"
  10. #import "UILabel+YBAttributeTextTapAction.h"
  11. @interface GroupSynopsisCell()
  12. @property (nonatomic, strong) NSArray * dataArray;
  13. @property (nonatomic, assign) BOOL show;
  14. @end
  15. @implementation GroupSynopsisCell
  16. - (void)awakeFromNib {
  17. [super awakeFromNib];
  18. self.iconV.layer.cornerRadius = 20.f;
  19. self.iconV.layer.masksToBounds = YES;
  20. self.collectionView.delegate = self;
  21. self.collectionView.dataSource = self;
  22. self.collectionView.scrollEnabled = NO;
  23. self.collectionView.showsVerticalScrollIndicator = NO;
  24. self.collectionView.showsHorizontalScrollIndicator = NO;
  25. [self.collectionView registerNib:[UINib nibWithNibName:@"SynopsisCollectCell" bundle:nil] forCellWithReuseIdentifier:@"SynopsisCollectCell"];
  26. self.layout = [[CollectionViewSpaceLayout alloc]init];
  27. self.layout.sectionInsets = UIEdgeInsetsMake(2, 15, 24, 15);
  28. self.layout.lineSpacing = 9.f;
  29. self.layout.interitemSpacing = 9.f;
  30. self.layout.delegate = self;
  31. [self.collectionView setCollectionViewLayout:self.layout];
  32. }
  33. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  34. [super setSelected:selected animated:animated];
  35. // Configure the view for the selected state
  36. }
  37. + (GroupSynopsisCell *)configCell0:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
  38. static NSString *cellIdentifer = @"GroupSynopsisCell0";
  39. GroupSynopsisCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
  40. if (cell == nil) {
  41. cell = [[[NSBundle mainBundle] loadNibNamed:@"GroupSynopsisCell" owner:nil options:nil] objectAtIndex:0];
  42. }
  43. return cell;
  44. }
  45. + (GroupSynopsisCell *)configCell1:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
  46. static NSString *cellIdentifer = @"GroupSynopsisCell1";
  47. GroupSynopsisCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
  48. if (cell == nil) {
  49. cell = [[[NSBundle mainBundle] loadNibNamed:@"GroupSynopsisCell" owner:nil options:nil] objectAtIndex:1];
  50. }
  51. return cell;
  52. }
  53. + (GroupSynopsisCell *)configCell2:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
  54. static NSString *cellIdentifer = @"GroupSynopsisCell2";
  55. GroupSynopsisCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
  56. if (cell == nil) {
  57. cell = [[[NSBundle mainBundle] loadNibNamed:@"GroupSynopsisCell" owner:nil options:nil] objectAtIndex:2];
  58. }
  59. return cell;
  60. }
  61. + (GroupSynopsisCell *)configCell3:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
  62. static NSString *cellIdentifer = @"GroupSynopsisCell3";
  63. GroupSynopsisCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
  64. if (cell == nil) {
  65. cell = [[[NSBundle mainBundle] loadNibNamed:@"GroupSynopsisCell" owner:nil options:nil] objectAtIndex:3];
  66. }
  67. return cell;
  68. }
  69. - (void)setData:(TDGroupInfoListModel *)model
  70. {
  71. CGFloat W = (SCREEN_WIDTH - 60 - 75) * 0.25;
  72. [self.BezierOneV addSubview:[self drawTotal:@100 current:@62 width:W color:UIColorHex(0xF53D23)]];
  73. [self.BezierTwoV addSubview:[self drawTotal:@100 current:@7 width:W color:UIColorHex(0x3B9DED)]];
  74. [self.BezierThreeV addSubview:[self drawTotal:@100 current:@31 width:W color:UIColorHex(0xFF9B30)]];
  75. [self.BezierFourV addSubview:[self drawTotal:@100 current:@20 width:W color:UIColorHex(0x91A5BD)]];
  76. self.BezierOneL.text = @"博士";
  77. self.BezierTwoL.text = @"北京";
  78. self.BezierThreeL.text = @"80后";
  79. self.BezierFourL.text = @"金融";
  80. [self setUserCount:model.CommonMemberCount];
  81. WS(weakSelf);
  82. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id _Nonnull sender) {
  83. if (weakSelf.ClickMoreBlcok) {
  84. weakSelf.ClickMoreBlcok();
  85. }
  86. }];
  87. [self.MoreView addGestureRecognizer:tap];
  88. self.MoreView.userInteractionEnabled = YES;
  89. }
  90. - (PNCircleChart *)drawTotal:(NSNumber *)total current:(NSNumber *)current width:(CGFloat)width color:(UIColor *)color
  91. {
  92. PNCircleChart * view = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 0, width, width)
  93. total:total
  94. current:current
  95. clockwise:YES
  96. shadow:YES
  97. shadowColor:UIColorHex(0xF1F1F1)
  98. displayCountingLabel:YES
  99. overrideLineWidth:@3.0f
  100. strokeColor:UIColorHex(0xF1F1F1)];
  101. view.backgroundColor = [UIColor whiteColor];
  102. [view setStrokeColor:color];
  103. [view setStrokeColorGradientStart:color];
  104. [view strokeChart];
  105. return view;
  106. }
  107. - (void)setDataModel:(TDGroupInfoListModel *)model isShow:(BOOL)show
  108. {
  109. self.show = show;
  110. self.contentL.numberOfLines = show ? 0 : 3;
  111. [self.iconV sd_setImageWithURL:[NSURL URLWithString:model.AvatarUrl] placeholderImage:IMG(@"img_placeHolder")];
  112. self.titleL.text = model.Name;
  113. self.nameL.text = model.UserName;
  114. [self setLabelwithText:model.Introduce];
  115. [self.contentL layoutIfNeeded];
  116. }
  117. //- (YYLabel *)contentL
  118. //{
  119. // if (!_contentL) {
  120. // _contentL = [YYLabel new];
  121. // }
  122. // return _contentL;;
  123. //}
  124. - (void)setUserCount:(NSInteger)count
  125. {
  126. self.countL.text = [NSString stringWithFormat:@"%ld人",count];
  127. }
  128. - (void)setGroupLabel:(NSString *)name
  129. {
  130. self.dataArray = [name componentsSeparatedByString:@","];
  131. [self.collectionView reloadData];
  132. [self.collectionView layoutIfNeeded];
  133. self.collectH.constant = self.collectionView.contentSize.height;
  134. }
  135. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  136. {
  137. return 9.f;
  138. }
  139. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  140. {
  141. return 9.f;
  142. }
  143. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  144. {
  145. return self.dataArray.count;
  146. }
  147. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  148. {
  149. return 1;
  150. }
  151. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  152. {
  153. UILabel * label = [UILabel new];
  154. label.text = self.dataArray[indexPath.item];
  155. label.font = Kfont(14);
  156. CGFloat W = [label sizeThatFits:CGSizeMake(MAXFLOAT, 24)].width + 24;
  157. return CGSizeMake(W, 24);
  158. }
  159. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  160. {
  161. SynopsisCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SynopsisCollectCell" forIndexPath:indexPath];
  162. cell.titleL.text = self.dataArray[indexPath.item];
  163. return cell;
  164. }
  165. - (void)setLabelwithText:(NSString *)text
  166. {
  167. if (text.length == 0) {
  168. self.contentL.text = @"";
  169. return;
  170. }
  171. NSArray * stringArr = [self getLinesArrayOfStringInText:text width:SCREEN_WIDTH - 30];
  172. if (stringArr.count > 3) {
  173. WS(weakSelf);
  174. if (self.show) {
  175. NSString *string4 = [NSString stringWithFormat:@"%@%@",text,self.show ? @"收起" : @"更多"];
  176. self.contentL.attributedText = [self AttributedwithText:string4 isShow:YES];
  177. [self.contentL yb_addAttributeTapActionWithRanges:@[NSStringFromRange(NSMakeRange(string4.length - 4, 4))] tapClicked:^(UILabel *label,NSString *string, NSRange range,NSInteger index) {
  178. NSLog(@"12313131");
  179. if (weakSelf.ShowBlcok) {
  180. weakSelf.ShowBlcok(!weakSelf.show);
  181. }
  182. }];
  183. }else{
  184. NSString *string1 = stringArr[0];
  185. NSString *string2 = stringArr[1];
  186. NSString *string3 = stringArr[2];
  187. string3 = [string3 substringToIndex:string3.length - 5];
  188. NSString *string4 = [NSString stringWithFormat:@"%@%@%@...%@",string1,string2,string3,self.show ? @"收起" : @"更多"];
  189. self.contentL.attributedText = [self AttributedwithText:string4 isShow:YES];
  190. [self.contentL yb_addAttributeTapActionWithRanges:@[NSStringFromRange(NSMakeRange(string4.length - 4, 4))] tapClicked:^(UILabel *label,NSString *string, NSRange range,NSInteger index) {
  191. NSLog(@"12313131");
  192. if (weakSelf.ShowBlcok) {
  193. weakSelf.ShowBlcok(!weakSelf.show);
  194. }
  195. }];
  196. }
  197. }else{
  198. self.contentL.attributedText = [self AttributedwithText:text isShow:NO];
  199. }
  200. }
  201. - (NSAttributedString *)AttributedwithText:(NSString *)text isShow:(BOOL)show
  202. {
  203. NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:text];
  204. [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, text.length)];
  205. [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(0x0a0a0a) range:NSMakeRange(0, text.length - (show ? 2 : 0))];
  206. if (show) {
  207. [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(0x3979D3) range:NSMakeRange(text.length - 2, 2)];
  208. }
  209. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
  210. paraStyle.alignment = NSTextAlignmentJustified;//两端对齐
  211. [paraStyle setLineSpacing:8];//行间距
  212. [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, text.length)];
  213. return mutableAttributedString;
  214. }
  215. - (NSArray *)getLinesArrayOfStringInText:(NSString *)text width:(CGFloat)width{
  216. UIFont *font = [UIFont systemFontOfSize:17];
  217. CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
  218. NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
  219. [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
  220. CFRelease(myFont);
  221. CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);
  222. CGMutablePathRef path = CGPathCreateMutable();
  223. CGPathAddRect(path, NULL, CGRectMake(0,0,width,MAXFLOAT));
  224. CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
  225. NSArray *lines = ( NSArray *)CTFrameGetLines(frame);
  226. NSMutableArray *linesArray = [[NSMutableArray alloc]init];
  227. for (id line in lines) {
  228. CTLineRef lineRef = (__bridge CTLineRef )line;
  229. CFRange lineRange = CTLineGetStringRange(lineRef);
  230. NSRange range = NSMakeRange(lineRange.location, lineRange.length);
  231. NSString *lineString = [text substringWithRange:range];
  232. CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));
  233. CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));
  234. [linesArray addObject:lineString];
  235. }
  236. CGPathRelease(path);
  237. CFRelease( frame );
  238. CFRelease(frameSetter);
  239. return (NSArray *)linesArray;
  240. }
  241. @end