// // MenuCell.swift // Sirani // // Created by Anhar Solehudin on 28/03/19. // Copyright © 2019 Anhar Solehudin. All rights reserved. // import UIKit import MaterialComponents.MaterialCards class MenuCell: UICollectionViewCell { private let menuItemSize = 85.75 private let iconSize = 36.0 func updateViews(menu: MenuItem) { // card let card = MDCCard() card.isInteractable = false card.backgroundColor = .white card.setShadowElevation(.init(1), for: .normal) card.setShadowColor(.gray, for: .normal) card.cornerRadius = 5 self.addSubview(card) card.anchor( top: topAnchor, bottom: bottomAnchor, leading: leadingAnchor, trailing: trailingAnchor, padding: .init(top: 4, left: 2, bottom: 4, right: 2) ) // icon let paddingSize = CGFloat((menuItemSize-iconSize-4)/2) let icon = UIImageView() icon.image = UIImage(named: menu.icon) icon.contentMode = .scaleAspectFit card.addSubview(icon) icon.anchor( top: card.topAnchor, bottom: nil, leading: card.leadingAnchor, trailing: card.trailingAnchor, padding: .init(top: 8, left: paddingSize, bottom: 0, right: paddingSize), size: .init(width: iconSize, height: iconSize) ) // name let nameLabel = UILabel() nameLabel.text = menu.name nameLabel.textAlignment = NSTextAlignment.center nameLabel.numberOfLines = 2 nameLabel.lineBreakMode = .byWordWrapping nameLabel.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) nameLabel.font = UIFont.systemFont(ofSize: 10, weight: .semibold) card.addSubview(nameLabel) nameLabel.anchor( top: icon.bottomAnchor, bottom: card.bottomAnchor, leading: card.leadingAnchor, trailing: card.trailingAnchor, padding: .init(top: 1, left: 2, bottom: 4, right: 2) ) // notif let totalNotif = menu.totalNotif if totalNotif > 0 { let bgNotif = UIView() bgNotif.backgroundColor = #colorLiteral(red: 0.8180381656, green: 0, blue: 0, alpha: 1) bgNotif.layer.cornerRadius = 5 card.addSubview(bgNotif) bgNotif.anchor( top: card.topAnchor, bottom: nil, leading: nil, trailing: card.trailingAnchor, padding: .init(top: 4, left: 0, bottom: 0, right: 4) ) bgNotif.bringSubviewToFront(icon) let notifLabel = UILabel() notifLabel.text = "\(totalNotif)" notifLabel.textAlignment = NSTextAlignment.center notifLabel.numberOfLines = 1 notifLabel.textColor = .white notifLabel.font = UIFont.systemFont(ofSize: 9, weight: .bold) bgNotif.addSubview(notifLabel) notifLabel.anchor( top: bgNotif.topAnchor, bottom: bgNotif.bottomAnchor, leading: bgNotif.leadingAnchor, trailing: bgNotif.trailingAnchor, padding: .init(top: 2, left: 4, bottom: 2, right: 4) ) } } }