Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FeedCell: UICollectionViewCell{
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupViews()
- }
- //Аватар автора
- let authorAvatar: UIImageView = {
- let iv = UIImageView()
- iv.image = UIImage(named: "author")
- iv.layer.cornerRadius = 15
- iv.layer.masksToBounds = true
- iv.translatesAutoresizingMaskIntoConstraints = false
- return iv
- }()
- //Имя автора
- let authorNamedLabel: UILabel = {
- let lb = UILabel()
- lb.text = "Иван Калягин"
- lb.font = lb.font.withSize(14)
- return lb
- }()
- let favoriteButtonStar: UIButton = {
- let bt = UIButton()
- bt.setImage(UIImage(named:"FeedStarOff"),for: .normal)
- return bt
- }()
- //Картинка статьи
- let thumbnailImageView: UIImageView = {
- let iv = UIImageView()
- iv.image = UIImage(named: "thumbnail")
- iv.clipsToBounds = true
- iv.contentMode = .scaleAspectFill
- // iv.clipsToBounds = true
- iv.translatesAutoresizingMaskIntoConstraints = true
- return iv
- }()
- //Заголовок статьи
- let titlePostLabel: UILabel = {
- let tv = UILabel()
- tv.text = "6 причин, почему не нужно следовать общественному мнению"
- tv.text = tv.text?.uppercased()
- tv.numberOfLines = 0
- tv.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
- tv.setLineSpacing(lineSpacing: 2.0)
- return tv
- }()
- let categoryPostLabel: UILabel = {
- let tv = UILabel()
- tv.text = "ОБРАЗ ЖИЗНИ"
- tv.text = tv.text?.uppercased()
- tv.numberOfLines = 0
- tv.font = UIFont.systemFont(ofSize: 12, weight: .semibold)
- tv.textColor = UIColor(displayP3Red: 0.64, green: 0.64, blue: 0.64, alpha: 1.0)
- return tv
- }()
- let dataPostLabel: UILabel = {
- let tv = UILabel()
- tv.text = "16.01.2018"
- tv.numberOfLines = 0
- tv.font = UIFont.systemFont(ofSize: 12, weight: .regular)
- tv.textColor = UIColor.black
- tv.textColor = UIColor(displayP3Red: 0.52, green: 0.52, blue: 0.52, alpha: 1.0)
- return tv
- }()
- let shortTextCut: UILabel = {
- let tv = UILabel()
- tv.text = "Сама наука восстала против этого, и теперь ты не будешь поддаваться мнению большинства, а будешь формировать собственные взгляды."
- tv.numberOfLines = 0
- tv.font = UIFont.systemFont(ofSize: 14, weight: .regular)
- return tv
- }()
- let commentsToPost: UILabel = {
- let tv = UILabel()
- tv.text = "5 Комментариев"
- tv.numberOfLines = 0
- tv.textColor = UIColor(displayP3Red: 0.52, green: 0.52, blue: 0.52, alpha: 1.0)
- tv.font = UIFont.systemFont(ofSize: 13, weight: .light)
- return tv
- }()
- let commentIcon: UIImageView = {
- let iv = UIImageView()
- iv.image = UIImage(named: "Comment")
- iv.translatesAutoresizingMaskIntoConstraints = false
- return iv
- }()
- func setupViews(){
- addSubview(authorAvatar)
- addSubview(authorNamedLabel)
- addSubview(favoriteButtonStar)
- addSubview(thumbnailImageView)
- addSubview(titlePostLabel)
- addSubview(categoryPostLabel)
- addSubview(dataPostLabel)
- addSubview(shortTextCut)
- addSubview(commentsToPost)
- addSubview(commentIcon)
- addConstraintWithFormat(format: "H:|-15-[v0(30)]-10-[v1]-[v2]-15-|", views: authorAvatar,authorNamedLabel,favoriteButtonStar)
- addConstraintWithFormat(format: "V:|-15-[v0(30)]", views: authorAvatar)
- addConstraintWithFormat(format: "V:|-15-[v0(30)]", views: authorAvatar)
- addConstraint(NSLayoutConstraint(item: authorNamedLabel, attribute: .top, relatedBy: .equal, toItem: authorAvatar, attribute: .top, multiplier: 1, constant: 6))
- addConstraint(NSLayoutConstraint(item: favoriteButtonStar, attribute: .top, relatedBy: .equal, toItem: authorAvatar, attribute: .top, multiplier: 1, constant: 0))
- addConstraintWithFormat(format: "H:|[v0]|", views: thumbnailImageView)
- addConstraint(NSLayoutConstraint(item: thumbnailImageView, attribute: .top, relatedBy: .equal, toItem: authorAvatar, attribute: .bottom, multiplier: 1, constant: 8))
- addConstraintWithFormat(format: "H:|-15-[v0]-15-|", views: titlePostLabel)
- addConstraint(NSLayoutConstraint(item: titlePostLabel, attribute: .top, relatedBy: .equal, toItem: thumbnailImageView, attribute: .bottom, multiplier: 1, constant: 13))
- addConstraintWithFormat(format: "H:|-15-[v0]-13-[v1]", views: categoryPostLabel, dataPostLabel)
- addConstraint(NSLayoutConstraint(item: categoryPostLabel, attribute: .top, relatedBy: .equal, toItem: titlePostLabel, attribute: .bottom, multiplier: 1, constant: 9))
- addConstraint(NSLayoutConstraint(item: dataPostLabel, attribute: .top, relatedBy: .equal, toItem: categoryPostLabel, attribute: .top, multiplier: 1, constant: 0))
- addConstraintWithFormat(format: "H:|-15-[v0]-15-|", views: shortTextCut)
- addConstraint(NSLayoutConstraint(item: shortTextCut, attribute: .top, relatedBy: .equal, toItem: categoryPostLabel, attribute: .bottom, multiplier: 1, constant: 10))
- addConstraintWithFormat(format: "H:|-15-[v0(12)]-7-[v1]|", views: commentIcon,commentsToPost)
- addConstraint(NSLayoutConstraint(item: commentIcon, attribute: .top, relatedBy: .equal, toItem: shortTextCut, attribute: .bottom, multiplier: 1, constant: 15))
- addConstraint(NSLayoutConstraint(item: commentIcon, attribute: .top, relatedBy: .equal, toItem: commentsToPost, attribute: .top, multiplier: 1, constant: 3))
- addConstraint(NSLayoutConstraint(item: commentIcon, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 12))
- addConstraint(NSLayoutConstraint(item: commentIcon, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 12))
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- class FeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
- override func viewDidLoad() {
- super.viewDidLoad()
- navigationItem.title = "Свежее"
- collectionView?.backgroundColor = UIColor.white
- collectionView?.showsHorizontalScrollIndicator = false
- collectionView?.showsVerticalScrollIndicator = false
- // Do any additional setup after loading the view, typically from a nib.
- collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: "cellId")
- }
- override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return 5
- }
- override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
- return cell
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- return CGSize(width: view.frame.width, height: 800)
- }
Add Comment
Please, Sign In to add comment