/************************************************************ * * Hyphenate CONFIDENTIAL * __________________ * Copyright (C) 2016 Hyphenate Inc. All rights reserved. * * NOTICE: All information contained herein is, and remains * the property of Hyphenate Inc. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from Hyphenate Inc. */ #import "EaseUserCell.h" #import "EaseImageView.h" #import "UIImageView+WebCache.h" CGFloat const EaseUserCellPadding = 10; @interface EaseUserCell() @property (nonatomic) NSLayoutConstraint *titleWithAvatarLeftConstraint; @property (nonatomic) NSLayoutConstraint *titleWithoutAvatarLeftConstraint; @end @implementation EaseUserCell + (void)initialize { // UIAppearance Proxy Defaults /** @brief 默认配置 */ EaseUserCell *cell = [self appearance]; cell.titleLabelColor = [UIColor blackColor]; cell.titleLabelFont = [UIFont systemFontOfSize:18]; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.accessibilityIdentifier = @"table_cell"; [self _setupSubview]; UILongPressGestureRecognizer *headerLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(headerLongPress:)]; [self addGestureRecognizer:headerLongPress]; } return self; } #pragma mark - private layout subviews /*! @method @brief 加载视图 @discussion @return */ - (void)_setupSubview { _avatarView = [[EaseImageView alloc] init]; _avatarView.translatesAutoresizingMaskIntoConstraints = NO; [self.contentView addSubview:_avatarView]; _titleLabel = [[UILabel alloc] init]; _titleLabel.accessibilityIdentifier = @"title"; _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; _titleLabel.numberOfLines = 2; _titleLabel.backgroundColor = [UIColor clearColor]; _titleLabel.font = _titleLabelFont; _titleLabel.textColor = _titleLabelColor; [self.contentView addSubview:_titleLabel]; [self _setupAvatarViewConstraints]; [self _setupTitleLabelConstraints]; } #pragma mark - Setup Constraints /*! @method @brief 设置avatarView的约束 @discussion @return */ - (void)_setupAvatarViewConstraints { [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:EaseUserCellPadding]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-EaseUserCellPadding]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:EaseUserCellPadding]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:self.avatarView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.avatarView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]]; } /*! @method @brief 设置titleLabel的约束 @discussion @return */ - (void)_setupTitleLabelConstraints { [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:EaseUserCellPadding]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-EaseUserCellPadding]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]]; [self addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-EaseUserCellPadding]]; self.titleWithAvatarLeftConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.avatarView attribute:NSLayoutAttributeRight multiplier:1.0 constant:EaseUserCellPadding]; self.titleWithoutAvatarLeftConstraint = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:EaseUserCellPadding]; [self addConstraint:self.titleWithAvatarLeftConstraint]; } #pragma mark - setter - (void)setShowAvatar:(BOOL)showAvatar { if (_showAvatar != showAvatar) { _showAvatar = showAvatar; self.avatarView.hidden = !showAvatar; if (_showAvatar) { [self removeConstraint:self.titleWithoutAvatarLeftConstraint]; [self addConstraint:self.titleWithAvatarLeftConstraint]; } else{ [self removeConstraint:self.titleWithAvatarLeftConstraint]; [self addConstraint:self.titleWithoutAvatarLeftConstraint]; } } } - (void)setModel:(id)model { _model = model; if ([_model.nickname length] > 0) { self.titleLabel.text = _model.nickname; } else{ self.titleLabel.text = _model.buddy; } if ([_model.avatarURLPath length] > 0){ [self.avatarView.imageView sd_setImageWithURL:[NSURL URLWithString:_model.avatarURLPath] placeholderImage:_model.avatarImage]; } else { if (_model.avatarImage) { self.avatarView.image = _model.avatarImage; } } } - (void)setTitleLabelFont:(UIFont *)titleLabelFont { _titleLabelFont = titleLabelFont; _titleLabel.font = _titleLabelFont; } - (void)setTitleLabelColor:(UIColor *)titleLabelColor { _titleLabelColor = titleLabelColor; _titleLabel.textColor = _titleLabelColor; } #pragma mark - class method /*! @method @brief 获取cell的重用标识 @discussion @param model 消息model @return 返回cell的重用标识 */ + (NSString *)cellIdentifierWithModel:(id)model { return @"EaseUserCell"; } /*! @method @brief 获取cell的高度 @discussion @param model 消息model @return 返回cell的高度 */ + (CGFloat)cellHeightWithModel:(id)model { return EaseUserCellMinHeight; } #pragma mark - action /*! @method @brief 头像长按事件 @discussion @param longPress 长按手势 @return */ - (void)headerLongPress:(UILongPressGestureRecognizer *)longPress { if (longPress.state == UIGestureRecognizerStateBegan) { if(_delegate && _indexPath && [_delegate respondsToSelector:@selector(cellLongPressAtIndexPath:)]) { [_delegate cellLongPressAtIndexPath:self.indexPath]; } } } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; if (_avatarView.badge) { _avatarView.badgeBackgroudColor = [UIColor redColor]; } } -(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{ [super setHighlighted:highlighted animated:animated]; if (_avatarView.badge) { _avatarView.badgeBackgroudColor = [UIColor redColor]; } } @end