self.container.layoutIfNeeded()
打印行之前
internal class PrologueTextView: UIView {
internal var labels: [UILabel] = []
internal let container: UIVisualEffectView = UIVisualEffectView()
// region #Properties
internal var shapeLayer: CAShapeLayer? {
return self.layer as? CAShapeLayer
}
internal override class var layerClass: AnyClass {
return CAShapeLayer.self
}
// endregion
// region #Initializers
internal override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
internal required init?(coder: NSCoder) {
super.init(coder: coder)
self.setup()
}
// endregion
// region #UIView lifecycle
internal override func layoutSubviews() {
super.layoutSubviews()
let mask: UIBezierPath = UIBezierPath()
for label in self.labels {
let roundedCorners = self.roundedCorners(for: label)
let maskBezierPath = UIBezierPath(roundedRect: label.frame, byRoundingCorners: roundedCorners, cornerRadii: CGSize(width: 20, height: 20))
mask.append(maskBezierPath)
}
self.shapeLayer?.path = mask.cgPath
self.container.layoutIfNeeded() // here
print("layoutSubviews(): \(self)")
print("layoutSubviews(): \(labels[0])")
print("layoutSubviews(): \(labels[1])")
print("layoutSubviews(): \(labels[2])")
}
// endregion
// region #Helper methods
private func setup() {
self.setupSubviews()
self.setupSubviewsAnchors()
}
private func setupSubviews() {
self.container.effect = UIBlurEffect(style: .light)
self.container.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.container)
let someSampleText = "This is\nsome sample\ntext for you"
for paragraph in someSampleText.components(separatedBy: "\n") {
let label = UILabel()
label.text = paragraph
label.translatesAutoresizingMaskIntoConstraints = false
self.labels.append(label)
self.container.contentView.addSubview(label)
}
}
private func setupSubviewsAnchors() {
NSLayoutConstraint.activate([
self.container.topAnchor.constraint(equalTo: self.topAnchor),
self.container.bottomAnchor.constraint(equalTo: self.bottomAnchor),
self.container.leadingAnchor.constraint(equalTo: self.leadingAnchor),
self.container.trailingAnchor.constraint(equalTo: self.trailingAnchor)
])
for (index, label) in self.labels.enumerated() {
let offset = 16.0 * CGFloat(index)
if index == 0 {
label.topAnchor.constraint(equalTo: self.container.contentView.topAnchor).isActive = true
} else {
let prev = self.labels[index - 1]
label.topAnchor.constraint(equalTo: prev.bottomAnchor).isActive = true
if index == self.labels.count - 1 {
label.bottomAnchor.constraint(equalTo: self.container.contentView.bottomAnchor).isActive = true
}
}
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: self.container.leadingAnchor, constant: offset),
label.trailingAnchor.constraint(lessThanOrEqualTo: self.container.trailingAnchor)])
}
}
private func roundedCorners(for label: UILabel) -> UIRectCorner {
switch label {
case self.labels.first:
return [.topLeft, .topRight, .bottomRight]
case self.labels.last:
return [.topRight, .bottomLeft, .bottomRight]
default:
return [.topRight, .bottomLeft]
}
}
// endregion
}