123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- //
- // FKCirclePublicImageCell.m
- // FirstLink
- //
- // Created by ascii on 16/6/8.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKCirclePublicImageCell.h"
- #import "FKCirclePublicImageItem.h"
- #import <SDWebImage/UIImageView+WebCache.h>
- #import "FLStringHelper.h"
- @interface FKCirclePublicImageCell ()
- @property (nonatomic, strong) UIImageView *displayImageView;
- @property (nonatomic, strong) UIButton *deleteButton;
- @property (nonatomic, strong) UIButton *textButton;
- @property (nonatomic, strong) UIButton *clipButton;
- @end
- @implementation FKCirclePublicImageCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
- self.selectionStyle = UITableViewCellSelectionStyleNone;
-
- [self.contentView addSubview:self.displayImageView];
-
- self.deleteButton = [self makeButtonWitImage:@"CircleImageDeleteIcon" tag:0];
- self.textButton = [self makeButtonWitImage:@"CircleImageTextIcon" tag:1];
- self.clipButton = [self makeButtonWitImage:@"CircleImageClipIcon" tag:2];
-
- [self.displayImageView addSubview:self.deleteButton];
- [self.displayImageView addSubview:self.textButton];
- [self.displayImageView addSubview:self.clipButton];
- }
- return self;
- }
- #pragma mark - API
- - (void)configWith:(id)item {
- if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
- FKCirclePublicImageItem *imageItem = (FKCirclePublicImageItem*)item;
- if (imageItem.displayImage) {
- self.displayImageView.image = imageItem.displayImage;
- } else if (imageItem.photoURL.length > 0) {
- [self.displayImageView sd_setImageWithURL:[NSURL URLWithString:imageItem.photoURL]
- completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- if (!error) {
- imageItem.sourceImage = image;
- imageItem.displayImage = image;
- }
- }];
- }
- [self.displayImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView).offset(0);
- make.top.equalTo(self.contentView).offset(0);
- make.right.equalTo(self.contentView).offset(0);
- make.height.mas_equalTo([FKCirclePublicImageCell imageHeightWith:imageItem]);
- }];
-
- // layout three button
- [self.deleteButton mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.displayImageView).offset(-15);
- make.bottom.equalTo(self.displayImageView).offset(-15);
- make.size.mas_equalTo(CGSizeMake(28, 28));
- }];
- [self.textButton mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.deleteButton.mas_left).offset(-10);
- make.centerY.equalTo(self.deleteButton);
- make.size.mas_equalTo(CGSizeMake(28, 28));
- }];
- [self.clipButton mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.textButton.mas_left).offset(-10);
- make.centerY.equalTo(self.deleteButton);
- make.size.mas_equalTo(CGSizeMake(28, 28));
- }];
-
- // layout textview
- if ([imageItem isNeedDisplay]) {
- [self.contentView addSubview:self.textView];
- self.textView.placeholder = imageItem.palceholder;
- self.textView.placeholderColor = UIColorFromRGB(0x999999);
- self.textView.text = imageItem.imageDesc;
-
- [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView).offset(12);
- make.bottom.equalTo(self.contentView).offset(-6);
- make.right.equalTo(self.contentView).offset(-12);
- // make.height.mas_equalTo([FKCirclePublicImageCell textHeight:imageItem]);
- }];
- } else {
- [self.textView removeFromSuperview];
- }
- }
- }
- #pragma mark - Method
- + (CGFloat)heightWith:(id)item {
- if ([item isKindOfClass:[FKCirclePublicImageItem class]]) {
- FKCirclePublicImageItem *imageItem = (FKCirclePublicImageItem*)item;
- CGFloat textHeight = [FKCirclePublicImageCell textHeight:imageItem];
- return ([FKCirclePublicImageCell imageHeightWith:imageItem] + (textHeight > 0 ? (15 + textHeight) : 0));
- }
- return 80;
- }
- + (CGFloat)imageHeightWith:(FKCirclePublicImageItem *)imageItem {
- if (!imageItem.displayImage) {
- return UISCREENWIDTH;
- }
- return ceilf(UISCREENWIDTH * imageItem.displayImage.size.height/imageItem.displayImage.size.width);
- }
- + (CGFloat)textHeight:(FKCirclePublicImageItem *)imageItem {
- if ([imageItem isNeedDisplay]) {
- UITextView *textView = [[UITextView alloc] init];
- textView.font = [UIFont systemFontOfSize:14];
- textView.text = imageItem.imageDesc;
-
- CGFloat width = (UISCREENWIDTH - 2 * 12);
- CGSize size = [textView sizeThatFits:CGSizeMake(width, MAXFLOAT)];
- return MAX(38, ceilf(size.height));
- }
- return 0;
- }
- #pragma mark - Action
- - (IBAction)clickButtonAction:(UIButton *)sender {
- if (self.clickCallback) {
- self.clickCallback((FKCirclePublicImageCellButton)sender.tag, [NSIndexPath indexPathForRow:0 inSection:self.tag]);
- }
- }
- - (void)setTag:(NSInteger)tag {
- [super setTag:tag];
-
- self.textView.tag = tag;
- }
- #pragma mark - Property
- - (UIImageView *)displayImageView {
- if (!_displayImageView) {
- _displayImageView = [UIImageView new];
- _displayImageView.userInteractionEnabled = YES;
- _displayImageView.contentMode = UIViewContentModeScaleAspectFit;
- }
- return _displayImageView;
- }
- - (UIPlaceHolderTextView *)textView {
- if (!_textView) {
- _textView = [[UIPlaceHolderTextView alloc] init];
- _textView.font = [UIFont systemFontOfSize:14];
- _textView.textColor = UIColorFromRGB(0x666666);
- _textView.scrollEnabled = NO;
- _textView.returnKeyType = UIReturnKeyDone;
- }
- return _textView;
- }
- - (UIButton *)makeButtonWitImage:(NSString *)imgName tag:(NSInteger)tag {
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.tag = tag;
- [button setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
-
- [button addTarget:self action:@selector(clickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- return button;
- }
- @end
|