From a4d2c1c250154014f67838825d51b6ba9f5c8c9c Mon Sep 17 00:00:00 2001 From: Momo Ozawa Date: Wed, 18 Oct 2023 16:48:55 +0100 Subject: [PATCH] combine title + snippet into a single attr string --- .../ViewRelated/Post/PostListCell.swift | 80 ++++++++----------- 1 file changed, 32 insertions(+), 48 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Post/PostListCell.swift b/WordPress/Classes/ViewRelated/Post/PostListCell.swift index bc66deb2a8b2..4d076654b01d 100644 --- a/WordPress/Classes/ViewRelated/Post/PostListCell.swift +++ b/WordPress/Classes/ViewRelated/Post/PostListCell.swift @@ -17,15 +17,8 @@ final class PostListCell: UITableViewCell, Reusable { return stackView }() - private lazy var textStackView: UIStackView = { - let stackView = UIStackView() - stackView.axis = .vertical - return stackView - }() - private let headerView = PostListHeaderView() - private let titleLabel = UILabel() - private let snippetLabel = UILabel() + private let titleAndSnippetLabel = UILabel() private let featuredImageView = CachedAnimatedImageView() private let statusLabel = UILabel() @@ -49,23 +42,8 @@ final class PostListCell: UITableViewCell, Reusable { func configure(with viewModel: PostListItemViewModel) { headerView.configure(with: viewModel) - if let title = viewModel.title, !title.isEmpty { - titleLabel.text = title - titleLabel.isHidden = false - } else { - titleLabel.isHidden = true - } - - if let snippet = viewModel.snippet, !snippet.isEmpty { - snippetLabel.text = snippet - snippetLabel.isHidden = false - } else { - snippetLabel.isHidden = true - } + configureTitleAndSnippet(with: viewModel) - titleLabel.numberOfLines = snippetLabel.isHidden ? 3 : 2 - snippetLabel.numberOfLines = titleLabel.isHidden ? 3 : 2 - imageLoader.prepareForReuse() featuredImageView.isHidden = viewModel.imageURL == nil if let imageURL = viewModel.imageURL { @@ -81,24 +59,40 @@ final class PostListCell: UITableViewCell, Reusable { statusLabel.isHidden = viewModel.status.isEmpty } + private func configureTitleAndSnippet(with viewModel: PostListItemViewModel) { + var titleAndSnippetString = NSMutableAttributedString() + + if let title = viewModel.title, !title.isEmpty { + let attributes: [NSAttributedString.Key: Any] = [ + .font: WPStyleGuide.fontForTextStyle(.callout, fontWeight: .semibold), + .foregroundColor: UIColor.text + ] + let titleAttributedString = NSAttributedString(string: "\(title)\n", attributes: attributes) + titleAndSnippetString.append(titleAttributedString) + } + + if let snippet = viewModel.snippet, !snippet.isEmpty { + let attributes: [NSAttributedString.Key: Any] = [ + .font: WPStyleGuide.fontForTextStyle(.footnote, fontWeight: .regular), + .foregroundColor: UIColor.textSubtle + ] + let snippetAttributedString = NSAttributedString(string: snippet, attributes: attributes) + titleAndSnippetString.append(snippetAttributedString) + } + + titleAndSnippetLabel.attributedText = titleAndSnippetString + } + // MARK: - Setup private func setupViews() { - setupTitleLabel() - setupSnippetLabel() + setupTitleAndSnippetLabel() setupFeaturedImageView() setupStatusLabel() - textStackView.translatesAutoresizingMaskIntoConstraints = false - textStackView.addArrangedSubviews([ - titleLabel, - snippetLabel - ]) - textStackView.spacing = 2 - contentStackView.translatesAutoresizingMaskIntoConstraints = false contentStackView.addArrangedSubviews([ - textStackView, + titleAndSnippetLabel, featuredImageView ]) contentStackView.spacing = 16 @@ -119,20 +113,10 @@ final class PostListCell: UITableViewCell, Reusable { contentView.backgroundColor = .systemBackground } - private func setupTitleLabel() { - titleLabel.translatesAutoresizingMaskIntoConstraints = false - titleLabel.adjustsFontForContentSizeCategory = true - titleLabel.numberOfLines = 2 - titleLabel.textColor = .text - titleLabel.font = WPStyleGuide.fontForTextStyle(.callout, fontWeight: .semibold) - } - - private func setupSnippetLabel() { - snippetLabel.translatesAutoresizingMaskIntoConstraints = false - snippetLabel.adjustsFontForContentSizeCategory = true - snippetLabel.numberOfLines = 2 - snippetLabel.textColor = .textSubtle - snippetLabel.font = WPStyleGuide.fontForTextStyle(.footnote, fontWeight: .regular) + private func setupTitleAndSnippetLabel() { + titleAndSnippetLabel.translatesAutoresizingMaskIntoConstraints = false + titleAndSnippetLabel.adjustsFontForContentSizeCategory = true + titleAndSnippetLabel.numberOfLines = 3 } private func setupFeaturedImageView() {