Last active
April 11, 2018 22:40
-
-
Save vhart/477a5cc903d4d6c72a5c0e18e05cbe2f to your computer and use it in GitHub Desktop.
Self sizing chat cells
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ChatCell: UITableViewCell { | |
lazy var iconImageView: UIImageView = { | |
let imageView = UIImageView() | |
let icon = UIImage(named: "icons8-Sheep on Bike") | |
imageView.translatesAutoresizingMaskIntoConstraints = false | |
imageView.backgroundColor = .clear | |
imageView.layer.cornerRadius = 25 | |
imageView.contentMode = .scaleAspectFit | |
imageView.image = icon | |
return imageView | |
}() | |
lazy var messageBackground: UIView = { | |
let backgroundView = UIView() | |
backgroundView.translatesAutoresizingMaskIntoConstraints = false | |
backgroundView.backgroundColor = #colorLiteral(red: 0, green: 0.5898008943, blue: 1, alpha: 1) | |
backgroundView.layer.cornerRadius = 10 | |
return backgroundView | |
}() | |
lazy var messageLabel: UILabel = { | |
let label = UILabel() | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.numberOfLines = 0 | |
label.backgroundColor = .clear | |
label.textColor = .white | |
return label | |
}() | |
override init(style: UITableViewCellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
layoutViews() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
layoutViews() | |
} | |
func layoutViews() { | |
contentView.addSubview(iconImageView) | |
contentView.addSubview(messageBackground) | |
messageBackground.addSubview(messageLabel) | |
NSLayoutConstraint.activate([ | |
iconImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8), | |
iconImageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8), | |
iconImageView.widthAnchor.constraint(equalToConstant: 50), | |
iconImageView.heightAnchor.constraint(equalTo: iconImageView.widthAnchor), | |
iconImageView.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -5), | |
messageBackground.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8), | |
messageBackground.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -8), | |
messageBackground.leadingAnchor.constraint(equalTo: iconImageView.trailingAnchor, constant: 12), | |
messageBackground.trailingAnchor.constraint(lessThanOrEqualTo: contentView.trailingAnchor, constant: -70), | |
messageLabel.trailingAnchor.constraint(lessThanOrEqualTo: messageBackground.trailingAnchor, constant: -5), | |
messageLabel.topAnchor.constraint(equalTo: messageBackground.topAnchor, constant: 5), | |
messageLabel.bottomAnchor.constraint(equalTo: messageBackground.bottomAnchor, constant: -5), | |
messageLabel.leadingAnchor.constraint(equalTo: messageBackground.leadingAnchor, constant: 5) | |
]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment