Skip to content

Facebook TabBarController 3 FeedController

Mohammad Azmal Hossain edited this page Feb 5, 2017 · 1 revision

import UIKit

let cellId = "cellId"

class Post { var name: String? var statusText: String? var profileImageName: String?

var statusImageName: String?
var numLikes: Int?
var numComments: Int?

}

class FeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout{

var posts = [Post]()
override func viewDidLoad() {
  super.viewDidLoad()
let postMark = Post()
postMark.name = "Mark Zuckerburg"
postMark.statusText = "Meanwhile, Beast turned to the dark side"
postMark.profileImageName = "taylorswift2"
postMark.numLikes = 1111
postMark.numComments = 11
let postSteve = Post()
postSteve.name = "Steve Jobs"
postSteve.statusText = "Design is not just what is looks like and feels like. Sesign is how it works \n\n" + "Being the richest man in the cemetery doesn't matter to me. Going to bes at night saying we've done soemthing wonderful, that's what matters to me. \n \n" + "Sometimes when you innovate, you make mistakes. it is best to adalt them quickly, and get on with improving you other innovations."
postSteve.profileImageName = "taylorswift2"
postSteve.numLikes = 2222
postSteve.numComments = 22
let postGandhi = Post()
postGandhi.name = "Mahatma Gandhi"
postGandhi.statusText = "Live as if you were to die tomorrow: learn as if you were to live forever. \n" + " The weak can never forgive. Forgiveness is the attribute of the stroing. \n" + "happiness is when what you think, what you say, and what you do are in harmany."
postGandhi.profileImageName = "taylorswift2"
postGandhi.numLikes = 3333
postGandhi.numComments = 33
posts.append(postMark)
posts.append(postSteve)
posts.append(postGandhi)
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 posts.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let feedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! FeedCell
feedCell.post = posts[indexPath.item]
  return feedCell
}
    // this func has in UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if let statusText = posts[indexPath.item].statusText {
// caculate size properly
let rect = NSString(string: statusText).boundingRect(with: CGSize(width: view.frame.width, height: 1000), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14)], context: nil)
let knownHeight : CGFloat = 8 + 44 + 4 + 4 + 200 + 8 + 24 + 8 + 44
    return CGSize(width: view.frame.width, height: rect.height + knownHeight + 24)
  }
  return CGSize(width: view.frame.width, height: 500)
}
// expand image in time of rotation
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  super.viewWillTransition(to: size, with: coordinator)
    collectionView?.collectionViewLayout.invalidateLayout()
  }
}

class FeedCell: UICollectionViewCell {

var post: Post? {
  didSet{
if let name = post?.name {
let attributedText = NSMutableAttributedString(string: name, attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 14)])
attributedText.append(NSAttributedString(string: "\n December 18 * San Francisco *", attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 12), NSForegroundColorAttributeName: UIColor.rgb(red: 155, green: 161, blue: 161)]))
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))
  nameLabel.attributedText = attributedText
}
if let statusText = post?.statusText {
  statusTextView.text = statusText
}
if let profileImageName2 = post?.profileImageName {
profileImageView.image = UIImage(named: profileImageName2)
}
    if let statusImageName = post?.statusImageName {
      statusImageView.image = UIImage(named: statusImageName)
    }
  }
}
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
  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)
  textView.isScrollEnabled = false
  return textView
}()
let statusImageView: UIImageView = {
    let imageView = UIImageView()
  imageView.image = UIImage(named: "taylorswiftfull")
  imageView.contentMode = .scaleAspectFill
  imageView.layer.masksToBounds = true
  return imageView
}()
let likesCommentsLabel: UILabel = {
  let label = UILabel()
  label.text = "488 Likes   10.7k"
  label.font = UIFont.systemFont(ofSize: 12)
  label.textColor = UIColor.rgb(red: 155, green: 161, blue: 171)
  return label
}()
let dividerLineView: UIView = {
let view = UIView()
  view.backgroundColor = UIColor.rgb(red: 226, green: 228, blue: 232)
  return view
}()
let likeButton = FeedCell.buttonForTitle(title: "Like", ImageName: "publiclogo")
let commentButton: UIButton = FeedCell.buttonForTitle(title: "Comment", ImageName: "publiclogo")
let shareButton: UIButton = FeedCell.buttonForTitle(title: "Share", ImageName: "publiclogo")
static func buttonForTitle(title: String, ImageName: String) -> UIButton {
  let button = UIButton()
  button.setTitle(title, for: .normal)
  button.setTitleColor(UIColor.rgb(red: 143, green: 150, blue: 263), for: .normal)
  button.setImage(UIImage(named: "publiclogo"), for: .normal) //put a like-button image
  button.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0)
  button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
  return button
}
func setupviews(){
  backgroundColor = UIColor.white
  addSubview(nameLabel)
  addSubview(profileImageView)
  addSubview(statusTextView)
  addSubview(statusImageView)
  addSubview(likesCommentsLabel)
  addSubview(dividerLineView)
  addSubview(likeButton)
  addSubview(commentButton)
  addSubview(shareButton)
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: "H:|-12-[v0]|", views: likesCommentsLabel)
addConstraintWithFormat(format: "H:|-12-[v0]-12-|", views: dividerLineView)
// button constraints
addConstraintWithFormat(format: "H:|[v0(v2)][v1(v2)][v2]|", views: likeButton, commentButton,shareButton)
addConstraintWithFormat(format: "V:|-12-[v0]", views: nameLabel)
    addConstraintWithFormat(format: "V:|-8-[v0(44)]-4-[v1]-4-[v2(200)]-8-[v3(24)]-8-[v4(0.4)]-8-[v5(44)]-8-|", views: profileImageView, statusTextView,statusImageView, likesCommentsLabel, dividerLineView, likeButton)
    addConstraintWithFormat(format: "V:[v0(44)]|", views: commentButton)
    addConstraintWithFormat(format: "V:[v0(44)]|", views: shareButton)
  }
}

extension UIColor {

  static func rgb(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
    return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1)
  }
}

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