Skip to content

Facebook ViewController 1

Mohammad Azmal Hossain edited this page Feb 3, 2017 · 3 revisions

import UIKit

class FeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout{

override func viewDidLoad() {
  super.viewDidLoad()
navigationItem.title = "FaceBook Feed"
// collectionView?.alwaysBounceHorizontal = true
collectionView?.alwaysBounceVertical = true
collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1)
  collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: "cellId")
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
 return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
}
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
                                  // this func has in UICollectionViewDelegateFlowLayout
    return CGSize(width: view.frame.width, height: 300)
  }
}

class FeedCell: UICollectionViewCell { override init(frame: CGRect){ super.init(frame: frame)

  setupviews()
}
required init?(coder aDecoder: NSCoder) {
  fatalError("init(coder:) has not been implemented")
}
let nameLabel:UILabel = {
  let label = UILabel()
  label.numberOfLines = 2
let attributedText = NSMutableAttributedString(string: "Mark Zuckerberg", attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 14)])
attributedText.append(NSAttributedString(string: "\n December 18 * San Francisco *", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 12), NSForegroundColorAttributeName: UIColor(red: 155/255, green: 161/255, blue: 171/255, alpha: 1)]))
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 4
attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedText.string.characters.count))
// for facebook public sign logo whick is like world logo
 let attachMent = NSTextAttachment()
 attachMent.image = UIImage(named: "publiclogo")
 attachMent.bounds = CGRect(x: 5, y: -2, width: 12, height: 12)
 attributedText.append(NSAttributedString(attachment: attachMent))
  label.attributedText = attributedText
  return label
}()
let profileImageView: UIImageView = {
 let imageView = UIImageView()
  imageView.image = UIImage(named: "taylorswift2")
  imageView.contentMode = .scaleAspectFit
//  imageView.backgroundColor = UIColor.green
  return imageView
}()
let statusTextView: UITextView = {
  let textView = UITextView()
  textView.text = "Meanwhile, Best turned to the dark side"
  textView.font = UIFont.systemFont(ofSize: 14)
  return textView
}()
let statusImageView: UIImageView = {
    let imageView = UIImageView()
  imageView.image = UIImage(named: "taylorswiftfull")
  imageView.contentMode = .scaleAspectFill
  imageView.layer.masksToBounds = true
  return imageView
}()
func setupviews(){
  backgroundColor = UIColor.white
  addSubview(nameLabel)
  addSubview(profileImageView)
  addSubview(statusTextView)
  addSubview(statusImageView)
addConstraintWithFormat(format: "H:|-8-[v0(44)]-8-[v1]|", views: profileImageView, nameLabel)
addConstraintWithFormat(format: "H:|-4-[v0]-4-|", views: statusTextView)
addConstraintWithFormat(format: "H:|[v0]|", views: statusImageView)
addConstraintWithFormat(format: "V:|-12-[v0]", views: nameLabel)
    addConstraintWithFormat(format: "V:|-8-[v0(44)]-4-[v1(30)]-4-[v2]|", views: profileImageView, statusTextView,statusImageView)
  }
}

extension UIView { func addConstraintWithFormat(format: String, views: UIView…​){ var viewsDictionary = [String: UIView]() for(index, view) in views.enumerated(){ let key = "v\(index)" viewsDictionary[key] = view view.translatesAutoresizingMaskIntoConstraints = false } addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary)) } }

Clone this wiki locally