Created
January 7, 2020 13:37
-
-
Save karthikgs7/94b071e4e07b6b5e08e91faba25106fd to your computer and use it in GitHub Desktop.
Titled View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private final class TopTitledView<T: UIView>: UIView { | |
private var titleLabel: UILabel! | |
private let contentView: T | |
private let title: String | |
init(contentView: T, title: String) { | |
self.contentView = contentView | |
self.title = title | |
super.init(frame: .zero) | |
setup() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func setup() { | |
setupTitleLabel() | |
configureTitleLabelConstraints() | |
setupContentView() | |
} | |
func setupTitleLabel() { | |
titleLabel = UILabel() | |
addSubview(titleLabel) | |
titleLabel.translatesAutoresizingMaskIntoConstraints = false | |
titleLabel.text = title | |
titleLabel.textAlignment = .center | |
titleLabel.font = UIFont.preferredFont(forTextStyle: .title1) | |
titleLabel.textColor = UIColor.black | |
titleLabel.numberOfLines = 0 | |
} | |
func configureTitleLabelConstraints() { | |
titleLabel.leading(equalTo: leadingAnchor, constant: 8.0) | |
titleLabel.trailing(equalTo: trailingAnchor, constant: -8.0) | |
titleLabel.top(equalTo: topAnchor, constant: 8.0) | |
titleLabel.enableContentHugging() | |
titleLabel.enableContentCompression() | |
} | |
func setupContentView() { | |
addSubview(contentView) | |
contentView.translatesAutoresizingMaskIntoConstraints = false | |
let topConstraint = contentView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8.0) | |
topConstraint.isActive = true | |
topConstraint.priority = UILayoutPriority(999) | |
contentView.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0.0).isActive = true | |
contentView.bottom(equalTo: bottomAnchor, constant: -8.0) | |
} | |
} | |
private final class BottomTitledView<T: UIView>: UIView { | |
private var titleLabel: UILabel! | |
private let contentView: T | |
private let title: String | |
init(contentView: T, title: String) { | |
self.contentView = contentView | |
self.title = title | |
super.init(frame: .zero) | |
setup() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func setup() { | |
setupTitleLabel() | |
setupContentView() | |
configureTitleLabelConstraints() | |
} | |
func setupTitleLabel() { | |
titleLabel = UILabel() | |
addSubview(titleLabel) | |
titleLabel.translatesAutoresizingMaskIntoConstraints = false | |
titleLabel.text = title | |
titleLabel.textAlignment = .center | |
titleLabel.font = UIFont.preferredFont(forTextStyle: .title1) | |
titleLabel.textColor = UIColor.black | |
titleLabel.numberOfLines = 0 | |
} | |
func configureTitleLabelConstraints() { | |
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8.0).isActive = true | |
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8.0).isActive = true | |
let topConstraint = titleLabel.topAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 8.0) | |
topConstraint.isActive = true | |
topConstraint.priority = UILayoutPriority(999) | |
titleLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8.0).isActive = true | |
titleLabel.enableContentHugging() | |
titleLabel.enableContentCompression() | |
} | |
func setupContentView() { | |
addSubview(contentView) | |
contentView.translatesAutoresizingMaskIntoConstraints = false | |
contentView.topAnchor.constraint(equalTo: topAnchor, constant: 8.0).isActive = true | |
contentView.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0.0).isActive = true | |
} | |
} | |
class TitledView<T: UIView>: UIView { | |
private var titleLabel: UILabel! | |
enum Position { | |
case top | |
case bottom | |
} | |
let contentView: T | |
let title: String | |
let position: Position | |
init(view: T, title: String, position: Position) { | |
self.contentView = view | |
self.title = title | |
self.position = position | |
super.init(frame: .zero) | |
setup() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override var intrinsicContentSize: CGSize { | |
var contentFrame: CGRect = .zero | |
for subview in subviews { | |
contentFrame = contentFrame.union(subview.frame) | |
} | |
return CGSize(width: contentFrame.width + 16.0, height: contentFrame.height + 16.0) | |
} | |
} | |
private extension TitledView { | |
func setup() { | |
let view: UIView | |
switch position { | |
case .top: | |
view = TopTitledView(contentView: contentView, title: title) | |
case .bottom: | |
view = BottomTitledView(contentView: contentView, title: title) | |
} | |
addSubview(view) | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.edges(equalTo: self, inset: .zero) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment