主要是cell中用法-----------当然这是对定高的cell而言的
1.cell的初始化方法中 所有的label只给frameCGRectZero,并且做相应属性的配置,
2.各控件的origin在layoutSubviews中来设置,
3.在cellforrow方法中利用sizeToFit来实现给label配置宽高,如果有别的要求比如label设置有最大宽度,可以在cell中再开放一个方法配置最大宽度
@implementation NIMSessionListCell
#define AvatarWidth 40
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_avatarImageView = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[self addSubview:_avatarImageView];
_nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_nameLabel.backgroundColor = [UIColor whiteColor];
_nameLabel.font = [UIFont systemFontOfSize:15.f];
[self addSubview:_nameLabel];
_messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_messageLabel.backgroundColor = [UIColor whiteColor];
_messageLabel.font = [UIFont systemFontOfSize:14.f];
_messageLabel.textColor = [UIColor lightGrayColor];
[self addSubview:_messageLabel];
_timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_timeLabel.backgroundColor = [UIColor whiteColor];
_timeLabel.font = [UIFont systemFontOfSize:14.f];
_timeLabel.textColor = [UIColor lightGrayColor];
[self addSubview:_timeLabel];
_badgeView = [NIMBadgeView viewWithBadgeTip:@""];
[self addSubview:_badgeView];
}
return self;
}
#define NameLabelMaxWidth 160.f
#define MessageLabelMaxWidth 200.f
- (void)refresh:(NIMRecentSession*)recent{
self.nameLabel.nim_width = self.nameLabel.nim_width > NameLabelMaxWidth ? NameLabelMaxWidth : self.nameLabel.nim_width;
self.messageLabel.nim_width = self.messageLabel.nim_width > MessageLabelMaxWidth ? MessageLabelMaxWidth : self.messageLabel.nim_width;
if (recent.unreadCount) {
self.badgeView.hidden = NO;
self.badgeView.badgeValue = @(recent.unreadCount).stringValue;
}else{
self.badgeView.hidden = YES;
}
}
- (void)layoutSubviews{
[super layoutSubviews];
//Session List
NSInteger sessionListAvatarLeft = 15;
NSInteger sessionListNameTop = 15;
NSInteger sessionListNameLeftToAvatar = 15;
NSInteger sessionListMessageLeftToAvatar = 15;
NSInteger sessionListMessageBottom = 15;
NSInteger sessionListTimeRight = 15;
NSInteger sessionListTimeTop = 15;
NSInteger sessionBadgeTimeBottom = 15;
NSInteger sessionBadgeTimeRight = 15;
_avatarImageView.nim_left = sessionListAvatarLeft;
_avatarImageView.nim_centerY = self.nim_height * .5f;
_nameLabel.nim_top = sessionListNameTop;
_nameLabel.nim_left = _avatarImageView.nim_right + sessionListNameLeftToAvatar;
_messageLabel.nim_left = _avatarImageView.nim_right + sessionListMessageLeftToAvatar;
_messageLabel.nim_bottom = self.nim_height - sessionListMessageBottom;
_timeLabel.nim_right = self.nim_width - sessionListTimeRight;
_timeLabel.nim_top = sessionListTimeTop;
_badgeView.nim_right = self.nim_width - sessionBadgeTimeRight;
_badgeView.nim_bottom = self.nim_height - sessionBadgeTimeBottom;
}
@implementation UIView (NIMKit)
- (CGFloat)nim_left {
return self.frame.origin.x;
}
///
- (void)setNim_left:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
///
- (CGFloat)nim_top {
return self.frame.origin.y;
}
///
- (void)setNim_top:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
///
- (CGFloat)nim_right {
return self.frame.origin.x + self.frame.size.width;
}
///
- (void)setNim_right:(CGFloat)right {
CGRect frame = self.frame;
frame.origin.x = right - frame.size.width;
self.frame = frame;
}
///
- (CGFloat)nim_bottom {
return self.frame.origin.y + self.frame.size.height;
}
///
- (void)setNim_bottom:(CGFloat)bottom {
CGRect frame = self.frame;
frame.origin.y = bottom - frame.size.height;
self.frame = frame;
}
///
- (CGFloat)nim_centerX {
return self.center.x;
}
///
- (void)setNim_centerX:(CGFloat)centerX {
self.center = CGPointMake(centerX, self.center.y);
}
///
- (CGFloat)nim_centerY {
return self.center.y;
}
///
- (void)setNim_centerY:(CGFloat)centerY {
self.center = CGPointMake(self.center.x, centerY);
}
///
- (CGFloat)nim_width {
return self.frame.size.width;
}
///
- (void)setNim_width:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
///
- (CGFloat)nim_height {
return self.frame.size.height;
}
///
- (void)setNim_height:(CGFloat)height {
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
tableview代理方法中的写法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellId = @"cellId";
NIMSessionListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[NIMSessionListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
[cell.avatarImageView addTarget:self action:@selector(onTouchAvatar:) forControlEvents:UIControlEventTouchUpInside];
}
NIMRecentSession *recent = self.recentSessions[indexPath.row];
cell.nameLabel.text = [self nameForRecentSession:recent];
[cell.avatarImageView setAvatarBySession:recent.session];
[cell.nameLabel sizeToFit];
cell.messageLabel.attributedText = [self contentForRecentSession:recent];
[cell.messageLabel sizeToFit];
cell.timeLabel.text = [self timestampDescriptionForRecentSession:recent];
[cell.timeLabel sizeToFit];
[cell refresh:recent];
return cell;
}