forked from tvidenov/appstore-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppCell.swift
99 lines (81 loc) · 3.25 KB
/
AppCell.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// AppCell.swift
// AppStore
//
// Created by Tihomir Videnov on 11/12/16.
// Copyright © 2016 Tihomir Videnov. All rights reserved.
//
import UIKit
class AppCell: UICollectionViewCell {
var app: App? {
didSet {
if let name = app?.name {
nameLabel.text = name
let rect = NSString(string: name).boundingRect(with: CGSize(width: frame.width,height: 1000), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14)], context: nil)
if rect.height > 20 {
categoryLabel.frame = CGRect(x: 0, y: frame.width + 38, width: frame.width, height: 20)
priceLabel.frame = CGRect(x: 0, y: frame.width + 56, width: frame.width, height: 20)
} else {
categoryLabel.frame = CGRect(x: 0, y: frame.width + 22, width: frame.width, height: 20)
priceLabel.frame = CGRect(x: 0, y: frame.width + 40, width: frame.width, height: 20)
}
nameLabel.frame = CGRect(x: 0, y: frame.width + 5, width: frame.width, height: 40)
nameLabel.sizeToFit()
}
categoryLabel.text = app?.category
if let price = app?.price {
priceLabel.text = "$\(price)"
} else {
priceLabel.text = ""
}
if let imageName = app?.imageName {
imageView.image = UIImage(named: imageName)
}
}
}
override init(frame: CGRect){
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let imageView: UIImageView = {
let iv = UIImageView()
//iv.image = UIImage(named: "frozen")
iv.contentMode = .scaleAspectFill
iv.layer.cornerRadius = 15
iv.layer.masksToBounds = true
return iv
}()
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Disney Build It: Frozen"
label.font = UIFont.systemFont(ofSize: 13)
label.numberOfLines = 2
return label
}()
let categoryLabel: UILabel = {
let label = UILabel()
label.text = "Entertaiment"
label.font = UIFont.systemFont(ofSize: 13)
return label
}()
let priceLabel: UILabel = {
let label = UILabel()
label.text = "$3.99"
label.font = UIFont.systemFont(ofSize: 13)
label.textColor = .darkGray
return label
}()
func setupViews() {
addSubview(imageView)
addSubview(nameLabel)
addSubview(categoryLabel)
addSubview(priceLabel)
imageView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.width)
nameLabel.frame = CGRect(x: 0, y: frame.width + 2, width: frame.width, height: 40)
categoryLabel.frame = CGRect(x: 0, y: frame.width + 38, width: frame.width, height: 20)
priceLabel.frame = CGRect(x: 0, y: frame.width + 56, width: frame.width, height: 20)
}
}