//
// ChatViewCell.swift
//
// Created by 柯木超 on 15/7/16.
// Copyright (c) 2015年 CCC. All rights reserved.
//
import UIKit
enum JLMessageType{
case me
case other
}
class ChatViewCell: UITableViewCell {
var MAXFLOAT:CGFloat = 0x1.fffffep+127 //文本最大高度
var _icon=UIImageView();
var _times=UILabel();
var _content:UIButton=UIButton.buttonWithType(UIButtonType.System) as! UIButton;
var bgImage = UIImageView();
var height:CGFloat=0;
var iconWidth:CGFloat=30;
var cellWidth:CGFloat=10;
var timeWidth:CGFloat=20;
var contentWidthWithBgImage:CGFloat=10;//文本与背景的边界距离
var JLTextFont:UIFont = UIFont(name: "HelveticaNeue",size: 15)!;
/**
* 是否隐藏时间(如果连续多条信息的时间相同,只能有一条信息显示时间)
*/
var hideTime:Bool?;
var chatMessageModel:ChatMessageModel?{
didSet{
self.addSubview(bgImage);
self.addSubview(_icon);
self.addSubview(_times);
self.addSubview(_content);
self.backgroundColor = UIColor.clearColor();
//填充内容
_icon.image=UIImage(named: "xiaohua");
var nf = NSDateFormatter();
nf.dateFromString("yyyy-MM-dd HH:mm")
_times.text = nf.stringFromDate(chatMessageModel!.time!);
_times.textAlignment = NSTextAlignment.Center;
_times.textColor = UIColor.blackColor()
// _content.backgroundColor = UIColor.yellowColor();
_content.setTitle(chatMessageModel?.content,forState: UIControlState.Normal);
_content.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
_content.titleLabel!.numberOfLines = 0;
// _content.titleLabel!.textColor = UIColor.blackColor()
_content.titleLabel!.font = UIFont(name: "HelveticaNeue",size: 14);
// 设置文本距离UIButton的边距
_content.contentEdgeInsets = UIEdgeInsetsMake(1.5*contentWidthWithBgImage,2*contentWidthWithBgImage,1.5*contentWidthWithBgImage,2*contentWidthWithBgImage)
//填充背景
if(chatMessageModel?.sourceId==7){ //7是本人,暂时先写成硬编码
bgImage.image = UIImage(named: "meme");
}else{
bgImage.image = UIImage(named: "other");
}
//算内容所占的空间
if(chatMessageModel?.sourceId==7){
//显示时间
if(chatMessageModel?.hideTime==1){
_times.frame = CGRect(x: (WIDTH-150)/2,y: 0,width: 150,height: timeWidth)
}
_icon.frame = CGRect(x: WIDTH-30*TMP-10*TMP,y: CGRectGetMaxY(_times.frame)+3*TMP,width: iconWidth,height: iconWidth)
//文本信息的最大宽度
var MAXWIDTH:CGFloat = WIDTH-30*TMP*2-50*TMP;
// 文本信息的最大尺寸
var textMaxSize = CGSizeMake(MAXWIDTH,MAXFLOAT);
var text:NSString = NSString(string: chatMessageModel!.content!);
var dict:NSDictionary = NSDictionary(object:JLTextFont,forKey: NSFontAttributeName)
//文本信息真实尺寸
var textRealSize = text.boundingRectWithSize(textMaxSize,options: NSStringDrawingOptions.UsesLineFragmentOrigin,attributes: dict as [NSObject : AnyObject],context: nil);
_content.frame = CGRect(x: WIDTH-textRealSize.width-iconWidth-55*TMP,y: CGRectGetMaxY(_icon.frame)-iconWidth,width: textRealSize.width+4*contentWidthWithBgImage,height: textRealSize.height+3*contentWidthWithBgImage);
_content.setBackgroundImage(UIImage.resizableImage("meme"),forState: UIControlState.Normal)
println("me icon===\(_icon)")
println("me _times=====\(_times)")
println("me content====\(_content)")
println("me bgimage==\(bgImage)");
height = CGRectGetMaxY(_content.frame);
}
//别人发的信息
else{
//显示时间
if(chatMessageModel?.hideTime==1){
_times.frame = CGRect(x: (WIDTH-150)/2,height: timeWidth)
}
_icon.frame = CGRect(x: 10*TMP,y: CGRectGetMaxY(_times.frame)+3*HeightTMP,height: iconWidth)
//文本信息的最大宽度
var MAXWIDTH:CGFloat = WIDTH-40*TMP*2-20*TMP*2;
// 文本信息的最大尺寸
var textMaxSize = CGSizeMake(MAXWIDTH,context: nil);
_content.frame = CGRect(x: 15*TMP+iconWidth,height: textRealSize.height+2*contentWidthWithBgImage);
_content.setBackgroundImage(UIImage.resizableImage("other"),forState: UIControlState.Normal)
// bgImage.frame = CGRect(x: 10*TMP+iconWidth,y: _content.frame.origin.y-10*TMP,width: _content.frame.size.width+30*TMP,height: _content.frame.size.height+20*TMP);
println("you icon===\(_icon)")
println("you _times=====\(_times)")
println("you content====\(_content)")
println("you bgimage==\(bgImage)");
height = CGRectGetMaxY(_content.frame);
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool,animated: Bool) {
super.setSelected(selected,animated: animated)
}
}