-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update course info header view layout (#1124)
* Add content stack view * Add title view * Fix dispatch hits to correct views
- Loading branch information
1 parent
0e183cd
commit c48bf06
Showing
4 changed files
with
204 additions
and
173 deletions.
There are no files selected for viewing
This file contains 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
107 changes: 107 additions & 0 deletions
107
Stepic/Sources/Modules/CourseInfo/Views/CourseInfoHeaderTitleView.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import SnapKit | ||
import UIKit | ||
|
||
extension CourseInfoHeaderTitleView { | ||
struct Appearance { | ||
let coverImageViewSize = CGSize(width: 32, height: 32) | ||
let coverImageViewCornerRadius: CGFloat = 6 | ||
|
||
let titleLabelFont = Typography.subheadlineFont | ||
let titleLabelColor = UIColor.white | ||
|
||
let spacing: CGFloat = 8 | ||
} | ||
} | ||
|
||
final class CourseInfoHeaderTitleView: UIView { | ||
let appearance: Appearance | ||
|
||
private lazy var coverImageView: CourseCoverImageView = { | ||
let view = CourseCoverImageView() | ||
view.clipsToBounds = true | ||
view.layer.cornerRadius = self.appearance.coverImageViewCornerRadius | ||
return view | ||
}() | ||
|
||
private lazy var titleLabel: UILabel = { | ||
let label = UILabel() | ||
label.font = self.appearance.titleLabelFont | ||
label.numberOfLines = 2 | ||
label.lineBreakMode = .byTruncatingTail | ||
label.textColor = self.appearance.titleLabelColor | ||
return label | ||
}() | ||
|
||
private lazy var containerView = UIView() | ||
|
||
var coverImageURL: URL? { | ||
didSet { | ||
self.coverImageView.loadImage(url: self.coverImageURL) | ||
} | ||
} | ||
|
||
var title: String? { | ||
didSet { | ||
self.titleLabel.text = self.title | ||
self.invalidateIntrinsicContentSize() | ||
} | ||
} | ||
|
||
override var intrinsicContentSize: CGSize { | ||
let height = max(self.appearance.coverImageViewSize.height, self.titleLabel.intrinsicContentSize.height) | ||
return CGSize(width: UIView.noIntrinsicMetric, height: height) | ||
} | ||
|
||
init( | ||
frame: CGRect = .zero, | ||
appearance: Appearance = Appearance() | ||
) { | ||
self.appearance = appearance | ||
super.init(frame: frame) | ||
|
||
self.setupView() | ||
self.addSubviews() | ||
self.makeConstraints() | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
} | ||
|
||
extension CourseInfoHeaderTitleView: ProgrammaticallyInitializableViewProtocol { | ||
func setupView() {} | ||
|
||
func addSubviews() { | ||
self.containerView.addSubview(self.coverImageView) | ||
self.containerView.addSubview(self.titleLabel) | ||
self.addSubview(self.containerView) | ||
} | ||
|
||
func makeConstraints() { | ||
self.containerView.translatesAutoresizingMaskIntoConstraints = false | ||
self.containerView.snp.makeConstraints { make in | ||
make.top.bottom.equalToSuperview() | ||
make.leading.greaterThanOrEqualToSuperview() | ||
make.trailing.lessThanOrEqualToSuperview() | ||
make.centerX.equalToSuperview() | ||
} | ||
|
||
self.coverImageView.translatesAutoresizingMaskIntoConstraints = false | ||
self.coverImageView.snp.makeConstraints { make in | ||
make.leading.greaterThanOrEqualToSuperview() | ||
make.centerY.equalToSuperview() | ||
make.size.equalTo(self.appearance.coverImageViewSize) | ||
} | ||
|
||
self.titleLabel.translatesAutoresizingMaskIntoConstraints = false | ||
self.titleLabel.setContentCompressionResistancePriority(.required, for: .horizontal) | ||
self.titleLabel.snp.makeConstraints { make in | ||
make.top.greaterThanOrEqualToSuperview() | ||
make.leading.equalTo(self.coverImageView.snp.trailing).offset(self.appearance.spacing) | ||
make.bottom.trailing.lessThanOrEqualToSuperview() | ||
make.centerY.equalToSuperview() | ||
} | ||
} | ||
} |
Oops, something went wrong.