标签:
---------- CZMessage.h ----------
#import <Foundation/Foundation.h>
typedef enum
{
CZMessageTypeMe = 0,
CZMessageTypeOther
}CZMessageType;
@interface CZMessage : NSObject
@property (copy, nonatomic) NSString *text;
@property (copy, nonatomic) NSString *time;
@property (assign, nonatomic) CZMessageType type;
@property (assign, nonatomic) BOOL hiddenTime;
+ (instancetype)messageWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
---------- CZMessage.m ----------
#import "CZMessage.h"
@implementation CZMessage
+ (instancetype)messageWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init])
{
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
@end
---------- CZMessageFrame.h ----------
#define CZTimeFont [UIFont systemFontOfSize:13]
#define CZTextFont [UIFont systemFontOfSize:15]
#import <UIKit/UIKit.h>
@class CZMessage;
@interface CZMessageFrame : NSObject
@property (strong, nonatomic) CZMessage *message;
@property (assign, nonatomic, readonly) CGRect timeF;
@property (assign, nonatomic, readonly) CGRect iconF;
@property (assign, nonatomic, readonly) CGRect textF;
@property (assign, nonatomic, readonly) CGFloat cellHeight;
@end
---------- CZMessageFrame.m ----------
#import "CZMessageFrame.h"
#import "CZMessage.h"
#define CZMargin 10
#define CZIconW 40
#define CZIconH 40
@implementation CZMessageFrame
- (void)setMessage:(CZMessage *)message
{
_message = message;
if (message.hiddenTime == NO)
{
CGFloat timeW = [UIScreen mainScreen].bounds.size.width;
CGFloat timeH = 35;
_timeF = CGRectMake(0, 0, timeW, timeH);
}
CGFloat iconX;
CGFloat iconY = CGRectGetMaxY(_timeF) + CZMargin;
CGFloat textY = iconY;
CGFloat textX;
CGFloat textMaxW = 150;
NSDictionary *attrs = @{ NSFontAttributeName : CZTextFont };
CGSize textSize = [message.text boundingRectWithSize:CGSizeMake(textMaxW, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
CGFloat textW = textSize.width + 40;
CGFloat textH = textSize.height + 30;
if (message.type == CZMessageTypeOther) {
iconX = CZMargin;
textX = iconX + CZIconW + CZMargin;
} else {
iconX = [UIScreen mainScreen].bounds.size.width - CZMargin - CZIconW;
textX = iconX - CZMargin - textW;
}
_iconF = CGRectMake(iconX, iconY, CZIconW, CZIconH);
_textF = CGRectMake(textX, textY, textW, textH);
CGFloat maxH = MAX(CGRectGetMaxY(_iconF), CGRectGetMaxY(_textF));
_cellHeight = maxH + CZMargin;
}
@end
标签:
原文地址:http://www.cnblogs.com/lixiang2015/p/4719810.html