Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
- @IBOutlet weak var collectionView: UICollectionView!
- @IBOutlet weak var label: UILabel!
- let now = Date()
- var cal = Calendar.current
- let dateFormatter = DateFormatter()
- var components = DateComponents()
- override func viewDidLoad() {
- super.viewDidLoad()
- cal.locale = Locale(identifier: "ja")
- dateFormatter.locale = Locale(identifier: "ja_JP")
- dateFormatter.dateFormat = "yyyy年M月"
- components.year = cal.component(.year, from: now)
- components.month = cal.component(.month, from: now)
- components.day = 1
- calculation()
- }
- func calculation(){
- let firstDayOfMonth = cal.date(from: components)
- label.text = dateFormatter.string(from: firstDayOfMonth!)
- }
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return 37
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
- let firstDayOfMonth = cal.date(from: components)
- let firstWeekday = cal.component(.weekday, from: firstDayOfMonth!)
- let weekdayAdding = 2 - firstWeekday
- for subview in cell.contentView.subviews {
- subview.removeFromSuperview()
- }
- let daysCountInMonth = cal.range(of: .day, in: .month, for: firstDayOfMonth!)?.count
- if (indexPath.row + weekdayAdding) >= 1 && (indexPath.row + weekdayAdding) <= daysCountInMonth! {
- cell.backgroundColor = #colorLiteral(red: 0.937254902, green: 0.937254902, blue: 0.9568627451, alpha: 1)
- let label = UILabel()
- label.font = UIFont(name: "Arial", size: 17)
- label.text = "\(indexPath.row + weekdayAdding)"
- label.sizeToFit()
- label.center = cell.contentView.center
- cell.contentView.addSubview(label)
- }
- else{
- cell.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
- }
- return cell
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement