Skip to content

Instantly share code, notes, and snippets.

@codetalks-new
Created January 10, 2015 00:19
Show Gist options
  • Save codetalks-new/686b01c17e157162acfd to your computer and use it in GitHub Desktop.
Save codetalks-new/686b01c17e157162acfd to your computer and use it in GitHub Desktop.
A Comment View based on UICollectionViewCell
class CommentView:UICollectionViewCell{
let nameView = UILabel()
let textLabel = UILabel()
class var identifier:String{
return "CommentView"
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
func setup(){
contentView.addSubview(nameView)
contentView.addSubview(textLabel)
nameView.setTranslatesAutoresizingMaskIntoConstraints(false)
textLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
textLabel.font = UIFont.systemFontOfSize(13)
textLabel.textColor = UIColor.darkTextColor()
nameView.font = UIFont.systemFontOfSize(12)
nameView.textColor = blueColor
let ntConstraint = NSLayoutConstraint(item: nameView, attribute: .Top, relatedBy: .Equal, toItem: contentView, attribute: .Top, multiplier: 1, constant: 8)
let nlConstraint = NSLayoutConstraint(item: nameView, attribute: .Leading, relatedBy: .Equal, toItem: contentView, attribute: .Leading, multiplier: 1, constant: 8)
let ttConstraint = NSLayoutConstraint(item: textLabel, attribute: .Top, relatedBy: .Equal, toItem: nameView, attribute: .Top, multiplier: 1, constant: 0)
let tlConstraint = NSLayoutConstraint(item: textLabel, attribute: .Leading, relatedBy: .Equal, toItem: nameView, attribute: .Trailing, multiplier: 1, constant: 4)
addConstraint(ntConstraint)
addConstraint(nlConstraint)
addConstraint(ttConstraint)
addConstraint(tlConstraint)
}
func bind(comment:Comment){
nameView.text = comment.name
textLabel.text = ":" + comment.text
layoutIfNeeded()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// debug comment view
let commentView = CommentView(frame: CGRect(x: 0, y: 0, width: 240, height: 42))
commentView.bind(Comment())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment