Skip to content

Commit

Permalink
combine title + snippet into a single attr string
Browse files Browse the repository at this point in the history
  • Loading branch information
Momo Ozawa committed Oct 18, 2023
1 parent a6762dc commit a4d2c1c
Showing 1 changed file with 32 additions and 48 deletions.
80 changes: 32 additions & 48 deletions WordPress/Classes/ViewRelated/Post/PostListCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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() {
Expand Down

0 comments on commit a4d2c1c

Please sign in to comment.