码迷,mamicode.com
首页 > 其他好文 > 详细

ChatCell

时间:2014-10-01 15:18:51      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:style   color   ar   sp   c   on   r   ad   ef   

import UIKit

let chatCellHeight: CGFloat = 72     

let chatCellInsetLeft =  chatCellHeight + 8

class ChatCell:UITableViewCell {

  let userPictureImageView:UIImageView

  let userNameLabel:UILabel

  let lastMessageTextLabel:UILabel

  let lastMessageSentDateLable:UILabel

  let userNameInitialsLabel:UILabel

  override init(style:UITableViewCellStyle, reuseIdentifier:String?) {

    let pictureSize:CGFloat = 64

    userPictureImageView = UIImageView(frame:CGRect(x:8,y:(chatCellHeight-pictureSize)/2,width:pictureSize,height:pictureSize))

    userPictureImageView.backgroundColor = UIColor(red:199/255.0,green:199/255.0,blue:204/255.0,alpha:1)

    userPictureImageView.layer.cornerRadius = pictureSize/2

    userPictureImageView.layer.masksToBounds = true//将位于它之下的层

    userNameLabel = UILabel(frame:CGRectZero)

    userNameLabel.backgroundColor = UIColor.whiteColor

    userNameLabel.font = UIFont.systemFontOfSize(17)

    

    lastMessageTextLabel = UILabel(frame:CGRectZero)

    lastMessageTextLabel.backgroundColor = UIColor.whiteColor()

    lastMessageTextLabel.font = UIFont.systemFontOfSize(15)

    lastMessageTextLabel.numberOfLines= 2

    lastMessageTextLabel.textColor = UIColor(red:142/255,green:142/255,blue:147/255,alpha:1)

    

    lastMessageSentDateLabel = UILabel(frame:CGRectZero)

    lastMessageSentDateLabel.autoresizingMask = .FlexibleLeftMargin

    lastMessageSentDateLabel.backgroundColor = UIColor.whiteColor()

    lastMessageSentDateLabel.font = UIFont.systemFontOfSize(15)

    lastMessageSentDateLabel.textColor = lastMessageTextLabel.textColor

 

    userNameInitialsLabel = UILabel(frame:CGRectZero)

    userNameInitialsLabel.font = UIFont.systemFontOfSize(33)

    userNameInitialsLabel.textAlignment = .Center

    userNameInitialsLabel.textColor = UIColor.whiteColor()

    

    super.init(style:style, reuseIdentifier:reuseIdentifier) 

    contentView.addSubView(userPictureImageView)

    contentView.addSubView(userNameLabel)

    contentView.addSubView(lastMessageTextLabel)

    contentView.addSubView(lastMessageSentDataLabel)

    userPictureImageView.addSubview(userNameInitialsLabel)

    

    userNameLabel.setTranslateAutoresizingMaskIntoConstraints(false)

    contentView.addConstraint(NSLayoutConstraint(item:userNameLabel,attribute:.Left,.Equal, toItem:contentView,attribute:.Left,multipler:1,constant:chatCellInsetLeft))

    contentView.addConstraint(NSlayoutContrstraint(item: userNameLabel,attribute:.Top,

    relatedBy:.Equal,toItem:contentView,attribute:.Top,multiplier:1,constant:6))

    lastMessageTextLabel.setTranslatesAutoresizingMaskIntoConstraints(true)

    super.init(style:style, reuseIdentifier:reuseIdentifier)

    contentView.

    lastMessageTextLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

    contentView.addConstraint(NSLayoutConstraint(item:lastMessageTextLabel,attribute:.Left,relatedBy:.Equal,toItem:userNameLabel,attribute:.Left,mltipllier:1,constrant:0))

    contentView.addConstraint(NSLayoutConstraint(item:lastMessageTextLabel,attribute:.Top,relatedBy:.Equal,toItem:contentView.,attribute:.Top,multiplier:1,constant:28))

contentView.addConstraint(NSLayoutConstraint(item:lastMessageTextLabel,attribute:.right,relatedBy:.Equal,toItem:contentView,attribute:.Right,multiplier:1,constant:-7))

contentView.addConstraint(NSLayoutConstraint(item:lastMessageTextLabel,attribute:.Bottom,relatedBy:.LessThanOrEquqal,toItem:contentView,attribute:.Bottom,multiplier:1,constant:-4))

lastMessageSentDateLabel.setTranslateAutoresizingMaskIntoConstrains(false)

contentView.addConstraint(NSLayoutConstraint(item:lastMessageSentDateLabel,attribute:.Left,elatedBy:.Equal,toItem:userNameLable,attribute:.Right,multiplierL1,constant:2))

contentView.addConstraint(NSLayoutConstraint(item:lastMessageSentDateLabel,attribute:.Right,relatedBy:.Equal,toItem:contentView,attribute:.Right,multipliter:1,constant:-7))

contentView.addConstraint(NSlabyoutConstraint(item:lastMessageSentDateLabel,attribute:.Baseline,relaedBy:.Equal,toItem:userNameLable,attrubute:.Baseline,multiplier:1,constant:0))

userNameInitialsLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

userPictureImageView.addConstraint(NSLayoutConstraint(item:userNameInitialsLabel,attribute:.CenterX,relatedBy:.Equal,toItem:userPictureImageView,attribute:.CenterX,multiplier:1,constant:0))

userPictureImageView.addConstraint(NSLayoutConstraint(item:userNameInitialsLabel,attribue:.CenterY,relatedBy:.Equal,toItem:userPictureImageView,attribute:.CenterY,multiplier:1,constant:-1))

  }

  required init(coder aDecoder:NSCoder) {

    fatalError("init(coder:) has not been implemented")

  }

 

  func configureWithChat(chat:Chat) {

    let user = chat.user

    userPictureImageView.image = UIImage(named:user.pictureName())

    if userPictureImageView.image == nil {

      let initials = user.initials

      if initials != nil {

        userNameInitialsLabel.text = initials

        userNameInitialsLabel.hidden = false

      } else {

        userPictureImageView.image = UIImage(named:"User0")

        userNameInitialsLabel.hidden = true

      }

    } else {

      userNameInitialsLabel.hidden = true

    }

    userNameLabel.text = user.name

    lastMessageTextLabel.text = chat.lastMessageText

    lastMessageSentDateLabel.text = chat.lastMessageSentDateString

  }

}

ChatCell

标签:style   color   ar   sp   c   on   r   ad   ef   

原文地址:http://www.cnblogs.com/yushunwu/p/4003163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!