From 8e1c044abc7ec9956c98b52489910567c92eeb38 Mon Sep 17 00:00:00 2001 From: Alexander Rutsman Date: Mon, 1 Nov 2021 23:05:27 +0300 Subject: [PATCH] feat: added TIScrollLabel --- .../Views/BaseInitializableScrollView.swift | 37 ++++++++ .../TIScrollLabel/LabeledScrollView.swift | 92 +++++++++++++++++++ .../Views/TIScrollLabel/TIScrollLabel.swift | 26 ++++++ 3 files changed, 155 insertions(+) create mode 100644 TIUIElements/Sources/Views/BaseInitializableScrollView.swift create mode 100644 TIUIElements/Sources/Views/TIScrollLabel/LabeledScrollView.swift create mode 100644 TIUIElements/Sources/Views/TIScrollLabel/TIScrollLabel.swift diff --git a/TIUIElements/Sources/Views/BaseInitializableScrollView.swift b/TIUIElements/Sources/Views/BaseInitializableScrollView.swift new file mode 100644 index 00000000..dbdf0b3b --- /dev/null +++ b/TIUIElements/Sources/Views/BaseInitializableScrollView.swift @@ -0,0 +1,37 @@ +import TIUIKitCore +import UIKit + +open class BaseInitializableScrollView: UIScrollView, InitializableViewProtocol { + override public init(frame: CGRect) { + super.init(frame: frame) + + initializeView() + } + + required public init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + + initializeView() + } + + // MARK: - InitializableView + open func addViews() { + // override in subclass + } + + open func configureLayout() { + // override in subclass + } + + open func bindViews() { + // override in subclass + } + + open func configureAppearance() { + // override in subclass + } + + open func localize() { + // override in subclass + } +} diff --git a/TIUIElements/Sources/Views/TIScrollLabel/LabeledScrollView.swift b/TIUIElements/Sources/Views/TIScrollLabel/LabeledScrollView.swift new file mode 100644 index 00000000..2dcbe123 --- /dev/null +++ b/TIUIElements/Sources/Views/TIScrollLabel/LabeledScrollView.swift @@ -0,0 +1,92 @@ +import UIKit +import SnapKit + +open class LabeledScrollView: BaseInitializableScrollView { + + private let textLabel = UILabel() + private let contentView = UIView() + + // MARK: - Configurable Properties + + public var text: String = "" { + didSet { + textLabel.text = text + } + } + + public var textColor: UIColor = .black { + didSet { + textLabel.textColor = textColor + } + } + + public var font: UIFont = UIFont.systemFont(ofSize: 13) { + didSet { + textLabel.font = font + } + } + + public var textAligment: NSTextAlignment = .center { + didSet { + textLabel.textAlignment = textAligment + } + } + + // MARK: - Initialization + + open override func addViews() { + super.addViews() + + addSubview(contentView) + contentView.addSubview(textLabel) + } + + open override func configureAppearance() { + super.configureAppearance() + + showsVerticalScrollIndicator = false + showsHorizontalScrollIndicator = false + backgroundColor = .clear + clipsToBounds = true + + textLabel.numberOfLines = 0 + textLabel.textAlignment = textAligment + textLabel.textColor = textColor + textLabel.font = font + } + + open override func configureLayout() { + super.configureLayout() + + contentView.translatesAutoresizingMaskIntoConstraints = false + textLabel.translatesAutoresizingMaskIntoConstraints = false + + let contentViewBottomConstraint = contentView.bottomAnchor.constraint(equalTo: bottomAnchor) + let contentViewCenterYConstraint = contentView.centerYAnchor.constraint(equalTo: centerYAnchor) + let contentViewHeightConstraint = contentView.heightAnchor.constraint(equalTo: heightAnchor) + + [ + contentViewBottomConstraint, + contentViewCenterYConstraint, + contentViewHeightConstraint + ].forEach { $0.priority = .defaultLow } + + NSLayoutConstraint.activate([ + contentView.leadingAnchor.constraint(equalTo: leadingAnchor), + contentView.trailingAnchor.constraint(equalTo: trailingAnchor), + contentView.topAnchor.constraint(equalTo: topAnchor), + contentView.centerXAnchor.constraint(equalTo: centerXAnchor), + contentViewBottomConstraint, + contentViewCenterYConstraint, + contentViewHeightConstraint + ]) + + NSLayoutConstraint.activate([ + textLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), + textLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), + textLabel.topAnchor.constraint(equalTo: contentView.topAnchor), + textLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), + textLabel.heightAnchor.constraint(equalTo: contentView.heightAnchor) + ]) + } +} diff --git a/TIUIElements/Sources/Views/TIScrollLabel/TIScrollLabel.swift b/TIUIElements/Sources/Views/TIScrollLabel/TIScrollLabel.swift new file mode 100644 index 00000000..96a43aa0 --- /dev/null +++ b/TIUIElements/Sources/Views/TIScrollLabel/TIScrollLabel.swift @@ -0,0 +1,26 @@ +import UIKit +import TIUIElements + +open class TIScrollLabel: BaseInitializableView { + + private let labeledScrollView = LabeledScrollView() + + open override func addViews() { + super.addViews() + + addSubview(labeledScrollView) + } + + open override func configureLayout() { + super.configureLayout() + + labeledScrollView.translatesAutoresizingMaskIntoConstraints = false + + NSLayoutConstraint.activate([ + labeledScrollView.leadingAnchor.constraint(equalTo: leadingAnchor), + labeledScrollView.trailingAnchor.constraint(equalTo: trailingAnchor), + labeledScrollView.topAnchor.constraint(equalTo: topAnchor), + labeledScrollView.bottomAnchor.constraint(equalTo: bottomAnchor) + ]) + } +}