| 1 |
//
|
|
| 2 |
// ChatLayout
|
|
| 3 |
// MessageContainerView.swift
|
|
| 4 |
// https://github.com/ekazaev/ChatLayout
|
|
| 5 |
//
|
|
| 6 |
// Created by Eugene Kazaev in 2020-2023.
|
|
| 7 |
// Distributed under the MIT license.
|
|
| 8 |
//
|
|
| 9 |
// Become a sponsor:
|
|
| 10 |
// https://github.com/sponsors/ekazaev
|
|
| 11 |
//
|
|
| 12 |
|
|
| 13 |
import Foundation
|
|
| 14 |
import UIKit
|
|
| 15 |
|
|
| 16 |
/// A container view that helps to layout the message view and its accessory
|
|
| 17 |
public final class MessageContainerView<AccessoryViewFactory: StaticViewFactory, MainView: UIView>: UIView {
|
|
| 18 |
|
|
| 19 |
private lazy var stackView = UIStackView(frame: bounds)
|
|
| 20 |
|
|
| 21 |
/// An accessory view.
|
|
| 22 |
public lazy var accessoryView: AccessoryViewFactory.View? = AccessoryViewFactory.buildView(within: bounds)
|
|
| 23 |
|
|
| 24 |
/// Main view.
|
|
| 25 |
public var customView: MainView {
|
! |
| 26 |
internalContentView.customView
|
! |
| 27 |
}
|
! |
| 28 |
|
|
| 29 |
/// An alignment of the contained views within the `MessageContainerView`,
|
|
| 30 |
public var alignment: ChatItemAlignment = .fullWidth {
|
! |
| 31 |
didSet {
|
! |
| 32 |
switch alignment {
|
! |
| 33 |
case .leading:
|
! |
| 34 |
internalContentView.flexibleEdges = [.trailing]
|
! |
| 35 |
case .trailing:
|
! |
| 36 |
internalContentView.flexibleEdges = [.leading]
|
! |
| 37 |
case .center:
|
! |
| 38 |
internalContentView.flexibleEdges = [.leading, .trailing]
|
! |
| 39 |
case .fullWidth:
|
! |
| 40 |
internalContentView.flexibleEdges = []
|
! |
| 41 |
}
|
! |
| 42 |
}
|
! |
| 43 |
}
|
|
| 44 |
|
|
| 45 |
private lazy var internalContentView = EdgeAligningView<MainView>(frame: bounds)
|
|
| 46 |
|
|
| 47 |
/// Initializes and returns a newly allocated view object with the specified frame rectangle.
|
|
| 48 |
/// - Parameter frame: The frame rectangle for the view, measured in points. The origin of the frame is relative
|
|
| 49 |
/// to the superview in which you plan to add it.
|
|
| 50 |
public override init(frame: CGRect) {
|
! |
| 51 |
super.init(frame: frame)
|
! |
| 52 |
setupSubviews()
|
! |
| 53 |
}
|
! |
| 54 |
|
|
| 55 |
/// Returns an object initialized from data in a given unarchiver.
|
|
| 56 |
/// - Parameter coder: An unarchiver object.
|
|
| 57 |
public required init?(coder: NSCoder) {
|
! |
| 58 |
super.init(coder: coder)
|
! |
| 59 |
setupSubviews()
|
! |
| 60 |
}
|
! |
| 61 |
|
|
| 62 |
private func setupSubviews() {
|
! |
| 63 |
translatesAutoresizingMaskIntoConstraints = false
|
! |
| 64 |
insetsLayoutMarginsFromSafeArea = false
|
! |
| 65 |
layoutMargins = .zero
|
! |
| 66 |
addSubview(stackView)
|
! |
| 67 |
|
! |
| 68 |
stackView.translatesAutoresizingMaskIntoConstraints = false
|
! |
| 69 |
stackView.axis = .horizontal
|
! |
| 70 |
stackView.spacing = .zero
|
! |
| 71 |
|
! |
| 72 |
NSLayoutConstraint.activate([
|
! |
| 73 |
stackView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
|
! |
| 74 |
stackView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
|
! |
| 75 |
stackView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
|
! |
| 76 |
stackView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor)
|
! |
| 77 |
])
|
! |
| 78 |
|
! |
| 79 |
if let accessoryView {
|
! |
| 80 |
stackView.addArrangedSubview(accessoryView)
|
! |
| 81 |
accessoryView.translatesAutoresizingMaskIntoConstraints = false
|
! |
| 82 |
}
|
! |
| 83 |
|
! |
| 84 |
internalContentView.translatesAutoresizingMaskIntoConstraints = false
|
! |
| 85 |
stackView.addArrangedSubview(internalContentView)
|
! |
| 86 |
}
|
! |
| 87 |
|
|
| 88 |
}
|
|