This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathBaseCommentCell.swift
178 lines (149 loc) · 8.18 KB
/
BaseCommentCell.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// BaseCommentCell.swift
// reddift
//
// Created by sonson on 2016/08/27.
// Copyright © 2016年 sonson. All rights reserved.
//
import Foundation
class BaseCommentCell: UITableViewCell {
let topInformationView: UIView = UIView(frame: CGRect.zero)
let verticalBar: UIView = UIView(frame: CGRect.zero)
var topInformationViewHeight: NSLayoutConstraint?
var varticalBarWidth: NSLayoutConstraint?
var varticalBarLeftMargin: NSLayoutConstraint?
let authorButton = UIButton(type: .custom)
let voteLabel = UILabel(frame: CGRect.zero)
let dateLabel = UILabel(frame: CGRect.zero)
let childlenLabel = UILabel(frame: CGRect.zero)
let toggleButton = UIButton(type: .custom)
var authorButtonWidth: NSLayoutConstraint?
var authorButtonHeight: NSLayoutConstraint?
var voteLabellWidth: NSLayoutConstraint?
var voteLabelHeight: NSLayoutConstraint?
var dateLabelWidth: NSLayoutConstraint?
var dateLabelHeight: NSLayoutConstraint?
var childlenLabelWidth: NSLayoutConstraint?
var childlenLabelHeight: NSLayoutConstraint?
var toggleButtonWidth: NSLayoutConstraint?
var toggleButtonHeight: NSLayoutConstraint?
static let verticalBarWidth = CGFloat(3)
static let informationViewHeight = CGFloat(37)
static let labelHeight = CGFloat(20)
static let leftMargin = CGFloat(5)
static let infoLabelHorizontalSpace = CGFloat(5)
static let expandIconWidth = CGFloat(20)
static let expandIconHeight = CGFloat(20)
weak var parentCommentViewController: CommentViewController?
@objc func didPushToggleButton(sender: Any) {
if let cc = parentCommentViewController {
cc.didPushToggleButtonOnCell(self)
}
}
class func estimateCommentAreaWidth(depth: Int, width: CGFloat) -> CGFloat {
// depth = 1 -> 5 (special case)
// depth = 2 -> 11
// depth = 3 -> 14
// depth = 5 -> 17
return width - (estimateBarLeftMargin(depth: depth) + leftMargin + verticalBarWidth)
}
class func estimateBarLeftMargin(depth: Int) -> CGFloat {
// depth = 1 -> -3 (special case)
// depth = 2 -> 3
// depth = 3 -> 6
// depth = 5 -> 9
switch depth {
case 1:
return -verticalBarWidth
default:
return (CGFloat(depth) - 1) * verticalBarWidth
}
}
func prepareLabel(label: UIView, baseView: UIView) -> (NSLayoutConstraint?, NSLayoutConstraint?) {
label.translatesAutoresizingMaskIntoConstraints = false
let widthConstraint = NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 0)
label.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: CommentCell.labelHeight)
label.addConstraint(heightConstraint)
let verticalCentering = NSLayoutConstraint(item: baseView, attribute: .centerY, relatedBy: .equal, toItem: label, attribute: .centerY, multiplier: 1, constant: 0)
baseView.addConstraint(verticalCentering)
return (widthConstraint, heightConstraint)
}
func updateSizeOfLabel(label: UILabel, widthConstraint: NSLayoutConstraint?, heightConstraint: NSLayoutConstraint?) {
let max = CGFloat.greatestFiniteMagnitude
let bounds = label.textRect(forBounds: CGRect(x: 0, y: 0, width: max, height: max), limitedToNumberOfLines: 1)
if let c = widthConstraint {
c.constant = bounds.size.width
}
if let c = heightConstraint {
c.constant = bounds.size.height
}
}
func updateSizeOfButton(button: UIButton, widthConstraint: NSLayoutConstraint?, heightConstraint: NSLayoutConstraint?) {
if let label = button.titleLabel {
let max = CGFloat.greatestFiniteMagnitude
let bounds = label.textRect(forBounds: CGRect(x: 0, y: 0, width: max, height: max), limitedToNumberOfLines: 1)
if let c = widthConstraint {
c.constant = bounds.size.width
}
if let c = heightConstraint {
c.constant = bounds.size.height
}
}
}
func prepareToggleButton() {
toggleButton.translatesAutoresizingMaskIntoConstraints = false
toggleButtonWidth = NSLayoutConstraint(item: toggleButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 0)
toggleButton.addConstraint(toggleButtonWidth!)
toggleButtonHeight = NSLayoutConstraint(item: toggleButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 0)
toggleButton.addConstraint(toggleButtonHeight!)
let verticalCentering = NSLayoutConstraint(item: topInformationView, attribute: .centerY, relatedBy: .equal, toItem: toggleButton, attribute: .centerY, multiplier: 1, constant: 0)
topInformationView.addConstraint(verticalCentering)
toggleButton.setImage(UIImage.init(named: "fold"), for: .normal)
toggleButtonWidth?.constant = BaseCommentCell.expandIconWidth
toggleButtonHeight?.constant = BaseCommentCell.expandIconHeight
}
func prepareInformationView() {
let views = [
"topInformationView": topInformationView,
"authorButton": authorButton,
"voteLabel": voteLabel,
"dateLabel": dateLabel,
"childlenLabel": childlenLabel,
"toggleButton": toggleButton
]
let metric = [
"space": CommentCell.infoLabelHorizontalSpace
]
authorButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
voteLabel.font = UIFont.systemFont(ofSize: 14)
dateLabel.font = UIFont.systemFont(ofSize: 14)
childlenLabel.font = UIFont.systemFont(ofSize: 14)
authorButton.setTitleColor(UIColor.black, for: [])
voteLabel.textColor = UIColor(red: 154/255.0, green: 169/255.0, blue: 180/255.0, alpha: 1)
dateLabel.textColor = UIColor(red: 154/255.0, green: 169/255.0, blue: 180/255.0, alpha: 1)
childlenLabel.textColor = UIColor(red: 154/255.0, green: 169/255.0, blue: 180/255.0, alpha: 1)
topInformationView.addSubview(authorButton)
topInformationView.addSubview(voteLabel)
topInformationView.addSubview(dateLabel)
topInformationView.addSubview(childlenLabel)
topInformationView.addSubview(toggleButton)
(authorButtonWidth, authorButtonHeight) = prepareLabel(label: authorButton, baseView: topInformationView)
(voteLabellWidth, voteLabelHeight) = prepareLabel(label: voteLabel, baseView: topInformationView)
(dateLabelWidth, dateLabelHeight) = prepareLabel(label: dateLabel, baseView: topInformationView)
(childlenLabelWidth, childlenLabelHeight) = prepareLabel(label: childlenLabel, baseView: topInformationView)
prepareToggleButton()
topInformationView.addConstraints (
NSLayoutConstraint.constraints(withVisualFormat: "H:|-space-[authorButton]-space-[voteLabel]-space-[dateLabel]-(>=0)-[childlenLabel]-space-[toggleButton]-space-|", options: NSLayoutFormatOptions(), metrics: metric, views: views)
)
voteLabel.text = "+100"
dateLabel.text = "3h"
updateSizeOfLabel(label: voteLabel, widthConstraint: voteLabellWidth, heightConstraint: voteLabelHeight)
updateSizeOfLabel(label: dateLabel, widthConstraint: dateLabelWidth, heightConstraint: dateLabelHeight)
authorButton.setTitle("sonson", for: [])
updateSizeOfButton(button: authorButton, widthConstraint: authorButtonWidth, heightConstraint: authorButtonHeight)
childlenLabel.text = ""
updateSizeOfLabel(label: childlenLabel, widthConstraint: childlenLabelWidth, heightConstraint: childlenLabelHeight)
toggleButton.addTarget(self, action: #selector(BaseCommentCell.didPushToggleButton(sender:)), for: .touchUpInside)
}
}