No Description

FKCirclePublicImageCell.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // FKCirclePublicImageCell.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/6/8.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKCirclePublicImageCell.h"
  9. #import "FKCirclePublicImageItem.h"
  10. #import <SDWebImage/UIImageView+WebCache.h>
  11. #import "FLStringHelper.h"
  12. @interface FKCirclePublicImageCell ()
  13. @property (nonatomic, strong) UIImageView *displayImageView;
  14. @property (nonatomic, strong) UIButton *deleteButton;
  15. @property (nonatomic, strong) UIButton *textButton;
  16. @property (nonatomic, strong) UIButton *clipButton;
  17. @end
  18. @implementation FKCirclePublicImageCell
  19. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  20. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  21. self.selectionStyle = UITableViewCellSelectionStyleNone;
  22. [self.contentView addSubview:self.displayImageView];
  23. self.deleteButton = [self makeButtonWitImage:@"CircleImageDeleteIcon" tag:0];
  24. self.textButton = [self makeButtonWitImage:@"CircleImageTextIcon" tag:1];
  25. self.clipButton = [self makeButtonWitImage:@"CircleImageClipIcon" tag:2];
  26. [self.displayImageView addSubview:self.deleteButton];
  27. [self.displayImageView addSubview:self.textButton];
  28. [self.displayImageView addSubview:self.clipButton];
  29. }
  30. return self;
  31. }
  32. #pragma mark - API
  33. - (void)configWith:(id)item {
  34. if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
  35. FKCirclePublicImageItem *imageItem = (FKCirclePublicImageItem*)item;
  36. if (imageItem.displayImage) {
  37. self.displayImageView.image = imageItem.displayImage;
  38. } else if (imageItem.photoURL.length > 0) {
  39. [self.displayImageView sd_setImageWithURL:[NSURL URLWithString:imageItem.photoURL]
  40. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  41. if (!error) {
  42. imageItem.sourceImage = image;
  43. imageItem.displayImage = image;
  44. }
  45. }];
  46. }
  47. [self.displayImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  48. make.left.equalTo(self.contentView).offset(0);
  49. make.top.equalTo(self.contentView).offset(0);
  50. make.right.equalTo(self.contentView).offset(0);
  51. make.height.mas_equalTo([FKCirclePublicImageCell imageHeightWith:imageItem]);
  52. }];
  53. // layout three button
  54. [self.deleteButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  55. make.right.equalTo(self.displayImageView).offset(-15);
  56. make.bottom.equalTo(self.displayImageView).offset(-15);
  57. make.size.mas_equalTo(CGSizeMake(28, 28));
  58. }];
  59. [self.textButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  60. make.right.equalTo(self.deleteButton.mas_left).offset(-10);
  61. make.centerY.equalTo(self.deleteButton);
  62. make.size.mas_equalTo(CGSizeMake(28, 28));
  63. }];
  64. [self.clipButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  65. make.right.equalTo(self.textButton.mas_left).offset(-10);
  66. make.centerY.equalTo(self.deleteButton);
  67. make.size.mas_equalTo(CGSizeMake(28, 28));
  68. }];
  69. // layout textview
  70. if ([imageItem isNeedDisplay]) {
  71. [self.contentView addSubview:self.textView];
  72. self.textView.placeholder = imageItem.palceholder;
  73. self.textView.placeholderColor = UIColorFromRGB(0x999999);
  74. self.textView.text = imageItem.imageDesc;
  75. [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
  76. make.left.equalTo(self.contentView).offset(12);
  77. make.bottom.equalTo(self.contentView).offset(-6);
  78. make.right.equalTo(self.contentView).offset(-12);
  79. // make.height.mas_equalTo([FKCirclePublicImageCell textHeight:imageItem]);
  80. }];
  81. } else {
  82. [self.textView removeFromSuperview];
  83. }
  84. }
  85. }
  86. #pragma mark - Method
  87. + (CGFloat)heightWith:(id)item {
  88. if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
  89. FKCirclePublicImageItem *imageItem = (FKCirclePublicImageItem*)item;
  90. CGFloat textHeight = [FKCirclePublicImageCell textHeight:imageItem];
  91. return ([FKCirclePublicImageCell imageHeightWith:imageItem] + (textHeight > 0 ? (15 + textHeight) : 0));
  92. }
  93. return 80;
  94. }
  95. + (CGFloat)imageHeightWith:(FKCirclePublicImageItem *)imageItem {
  96. if (!imageItem.displayImage) {
  97. return UISCREENWIDTH;
  98. }
  99. return ceilf(UISCREENWIDTH * imageItem.displayImage.size.height/imageItem.displayImage.size.width);
  100. }
  101. + (CGFloat)textHeight:(FKCirclePublicImageItem *)imageItem {
  102. if ([imageItem isNeedDisplay]) {
  103. UITextView *textView = [[UITextView alloc] init];
  104. textView.font = [UIFont systemFontOfSize:14];
  105. textView.text = imageItem.imageDesc;
  106. CGFloat width = (UISCREENWIDTH - 2 * 12);
  107. CGSize size = [textView sizeThatFits:CGSizeMake(width, MAXFLOAT)];
  108. return MAX(38, ceilf(size.height));
  109. }
  110. return 0;
  111. }
  112. #pragma mark - Action
  113. - (IBAction)clickButtonAction:(UIButton *)sender {
  114. if (self.clickCallback) {
  115. self.clickCallback((FKCirclePublicImageCellButton)sender.tag, [NSIndexPath indexPathForRow:0 inSection:self.tag]);
  116. }
  117. }
  118. - (void)setTag:(NSInteger)tag {
  119. [super setTag:tag];
  120. self.textView.tag = tag;
  121. }
  122. #pragma mark - Property
  123. - (UIImageView *)displayImageView {
  124. if (!_displayImageView) {
  125. _displayImageView = [UIImageView new];
  126. _displayImageView.userInteractionEnabled = YES;
  127. _displayImageView.contentMode = UIViewContentModeScaleAspectFit;
  128. }
  129. return _displayImageView;
  130. }
  131. - (UIPlaceHolderTextView *)textView {
  132. if (!_textView) {
  133. _textView = [[UIPlaceHolderTextView alloc] init];
  134. _textView.font = [UIFont systemFontOfSize:14];
  135. _textView.textColor = UIColorFromRGB(0x666666);
  136. _textView.scrollEnabled = NO;
  137. _textView.returnKeyType = UIReturnKeyDone;
  138. }
  139. return _textView;
  140. }
  141. - (UIButton *)makeButtonWitImage:(NSString *)imgName tag:(NSInteger)tag {
  142. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  143. button.tag = tag;
  144. [button setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
  145. [button addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  146. return button;
  147. }
  148. @end