Guest User

Untitled

a guest
Jan 24th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. class FeedCell: UICollectionViewCell{
  2. override init(frame: CGRect) {
  3. super.init(frame: frame)
  4. setupViews()
  5. }
  6.  
  7. //Аватар автора
  8. let authorAvatar: UIImageView = {
  9. let iv = UIImageView()
  10. iv.image = UIImage(named: "author")
  11. iv.layer.cornerRadius = 15
  12. iv.layer.masksToBounds = true
  13. iv.translatesAutoresizingMaskIntoConstraints = false
  14. return iv
  15. }()
  16.  
  17. //Имя автора
  18. let authorNamedLabel: UILabel = {
  19. let lb = UILabel()
  20. lb.text = "Иван Калягин"
  21. lb.font = lb.font.withSize(14)
  22. return lb
  23. }()
  24.  
  25. let favoriteButtonStar: UIButton = {
  26. let bt = UIButton()
  27. bt.setImage(UIImage(named:"FeedStarOff"),for: .normal)
  28. return bt
  29. }()
  30.  
  31. //Картинка статьи
  32. let thumbnailImageView: UIImageView = {
  33. let iv = UIImageView()
  34. iv.image = UIImage(named: "thumbnail")
  35. iv.clipsToBounds = true
  36. iv.contentMode = .scaleAspectFill
  37. // iv.clipsToBounds = true
  38. iv.translatesAutoresizingMaskIntoConstraints = true
  39. return iv
  40. }()
  41.  
  42. //Заголовок статьи
  43. let titlePostLabel: UILabel = {
  44.  
  45. let tv = UILabel()
  46. tv.text = "6 причин, почему не нужно следовать общественному мнению"
  47. tv.text = tv.text?.uppercased()
  48. tv.numberOfLines = 0
  49. tv.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
  50. tv.setLineSpacing(lineSpacing: 2.0)
  51.  
  52. return tv
  53. }()
  54.  
  55. let categoryPostLabel: UILabel = {
  56. let tv = UILabel()
  57. tv.text = "ОБРАЗ ЖИЗНИ"
  58. tv.text = tv.text?.uppercased()
  59. tv.numberOfLines = 0
  60. tv.font = UIFont.systemFont(ofSize: 12, weight: .semibold)
  61. tv.textColor = UIColor(displayP3Red: 0.64, green: 0.64, blue: 0.64, alpha: 1.0)
  62.  
  63. return tv
  64. }()
  65. let dataPostLabel: UILabel = {
  66. let tv = UILabel()
  67. tv.text = "16.01.2018"
  68. tv.numberOfLines = 0
  69. tv.font = UIFont.systemFont(ofSize: 12, weight: .regular)
  70. tv.textColor = UIColor.black
  71. tv.textColor = UIColor(displayP3Red: 0.52, green: 0.52, blue: 0.52, alpha: 1.0)
  72.  
  73. return tv
  74. }()
  75.  
  76. let shortTextCut: UILabel = {
  77. let tv = UILabel()
  78. tv.text = "Сама наука восстала против этого, и теперь ты не будешь поддаваться мнению большинства, а будешь формировать собственные взгляды."
  79. tv.numberOfLines = 0
  80. tv.font = UIFont.systemFont(ofSize: 14, weight: .regular)
  81.  
  82. return tv
  83. }()
  84.  
  85. let commentsToPost: UILabel = {
  86. let tv = UILabel()
  87. tv.text = "5 Комментариев"
  88. tv.numberOfLines = 0
  89. tv.textColor = UIColor(displayP3Red: 0.52, green: 0.52, blue: 0.52, alpha: 1.0)
  90. tv.font = UIFont.systemFont(ofSize: 13, weight: .light)
  91.  
  92. return tv
  93. }()
  94.  
  95. let commentIcon: UIImageView = {
  96. let iv = UIImageView()
  97. iv.image = UIImage(named: "Comment")
  98. iv.translatesAutoresizingMaskIntoConstraints = false
  99. return iv
  100. }()
  101.  
  102. func setupViews(){
  103. addSubview(authorAvatar)
  104. addSubview(authorNamedLabel)
  105. addSubview(favoriteButtonStar)
  106. addSubview(thumbnailImageView)
  107. addSubview(titlePostLabel)
  108. addSubview(categoryPostLabel)
  109. addSubview(dataPostLabel)
  110. addSubview(shortTextCut)
  111. addSubview(commentsToPost)
  112. addSubview(commentIcon)
  113.  
  114. addConstraintWithFormat(format: "H:|-15-[v0(30)]-10-[v1]-[v2]-15-|", views: authorAvatar,authorNamedLabel,favoriteButtonStar)
  115. addConstraintWithFormat(format: "V:|-15-[v0(30)]", views: authorAvatar)
  116. addConstraintWithFormat(format: "V:|-15-[v0(30)]", views: authorAvatar)
  117. addConstraint(NSLayoutConstraint(item: authorNamedLabel, attribute: .top, relatedBy: .equal, toItem: authorAvatar, attribute: .top, multiplier: 1, constant: 6))
  118. addConstraint(NSLayoutConstraint(item: favoriteButtonStar, attribute: .top, relatedBy: .equal, toItem: authorAvatar, attribute: .top, multiplier: 1, constant: 0))
  119.  
  120. addConstraintWithFormat(format: "H:|[v0]|", views: thumbnailImageView)
  121. addConstraint(NSLayoutConstraint(item: thumbnailImageView, attribute: .top, relatedBy: .equal, toItem: authorAvatar, attribute: .bottom, multiplier: 1, constant: 8))
  122.  
  123. addConstraintWithFormat(format: "H:|-15-[v0]-15-|", views: titlePostLabel)
  124. addConstraint(NSLayoutConstraint(item: titlePostLabel, attribute: .top, relatedBy: .equal, toItem: thumbnailImageView, attribute: .bottom, multiplier: 1, constant: 13))
  125.  
  126. addConstraintWithFormat(format: "H:|-15-[v0]-13-[v1]", views: categoryPostLabel, dataPostLabel)
  127. addConstraint(NSLayoutConstraint(item: categoryPostLabel, attribute: .top, relatedBy: .equal, toItem: titlePostLabel, attribute: .bottom, multiplier: 1, constant: 9))
  128. addConstraint(NSLayoutConstraint(item: dataPostLabel, attribute: .top, relatedBy: .equal, toItem: categoryPostLabel, attribute: .top, multiplier: 1, constant: 0))
  129.  
  130. addConstraintWithFormat(format: "H:|-15-[v0]-15-|", views: shortTextCut)
  131. addConstraint(NSLayoutConstraint(item: shortTextCut, attribute: .top, relatedBy: .equal, toItem: categoryPostLabel, attribute: .bottom, multiplier: 1, constant: 10))
  132.  
  133. addConstraintWithFormat(format: "H:|-15-[v0(12)]-7-[v1]|", views: commentIcon,commentsToPost)
  134. addConstraint(NSLayoutConstraint(item: commentIcon, attribute: .top, relatedBy: .equal, toItem: shortTextCut, attribute: .bottom, multiplier: 1, constant: 15))
  135. addConstraint(NSLayoutConstraint(item: commentIcon, attribute: .top, relatedBy: .equal, toItem: commentsToPost, attribute: .top, multiplier: 1, constant: 3))
  136. addConstraint(NSLayoutConstraint(item: commentIcon, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 12))
  137. addConstraint(NSLayoutConstraint(item: commentIcon, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 12))
  138. }
  139.  
  140. required init?(coder aDecoder: NSCoder) {
  141. fatalError("init(coder:) has not been implemented")
  142. }
  143.  
  144. class FeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
  145.  
  146. override func viewDidLoad() {
  147. super.viewDidLoad()
  148.  
  149. navigationItem.title = "Свежее"
  150.  
  151. collectionView?.backgroundColor = UIColor.white
  152. collectionView?.showsHorizontalScrollIndicator = false
  153. collectionView?.showsVerticalScrollIndicator = false
  154. // Do any additional setup after loading the view, typically from a nib.
  155.  
  156. collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: "cellId")
  157. }
  158.  
  159. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  160. return 5
  161. }
  162.  
  163. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  164. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
  165.  
  166. return cell
  167. }
  168.  
  169. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  170.  
  171.  
  172.  
  173.  
  174.  
  175. return CGSize(width: view.frame.width, height: 800)
  176. }
Add Comment
Please, Sign In to add comment