From 5d31e89077ad58aa1000efc64a40cf0a221a9014 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Mon, 26 Jan 2026 20:46:28 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Hierarchical=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iOS/MainTimeline/Cells/MainTimelineCell.swift | 274 ++++++++++-------- .../MainTimelineCollectionViewCell.swift | 48 ++- 2 files changed, 169 insertions(+), 153 deletions(-) diff --git a/iOS/MainTimeline/Cells/MainTimelineCell.swift b/iOS/MainTimeline/Cells/MainTimelineCell.swift index 8bf2e3de0..3627547f7 100644 --- a/iOS/MainTimeline/Cells/MainTimelineCell.swift +++ b/iOS/MainTimeline/Cells/MainTimelineCell.swift @@ -21,11 +21,9 @@ final class MainTimelineCell: UITableViewCell { configure(cellData) } } - + var isPreview: Bool = false - - private static let indicatorAnimationDuration = 0.25 - + override func awakeFromNib() { MainActor.assumeIsolated { super.awakeFromNib() @@ -34,89 +32,60 @@ final class MainTimelineCell: UITableViewCell { configureStackView() } } - - func setIconImage(_ iconImage: IconImage?) { - guard let feedIcon else { - return - } - if feedIcon.iconImage !== iconImage { - feedIcon.iconImage = iconImage - } + + override func layoutSubviews() { + super.layoutSubviews() } - - override func updateConfiguration(using state: UICellConfigurationState) { - super.updateConfiguration(using: state) - - var backgroundConfig = UIBackgroundConfiguration.listCell().updated(for: state) - backgroundConfig.cornerRadius = 20 - if traitCollection.userInterfaceIdiom == .pad { - backgroundConfig.edgesAddingLayoutMarginsToBackgroundInsets = [.leading, .trailing] - backgroundConfig.backgroundInsets = NSDirectionalEdgeInsets(top: 0, leading: !isPreview ? -4 : -12, bottom: 0, trailing: !isPreview ? -4 : -12) - } - - articleContent.textColor = titleTextColor(for: state) - if isActive(state) { - backgroundConfig.backgroundColor = Assets.Colors.primaryAccent - articleDate.textColor = .lightText - articleByLine.textColor = .lightText - } else { - articleDate.textColor = .secondaryLabel - articleByLine.textColor = .secondaryLabel - } - - updateIndicatorView(state) - - backgroundConfiguration = backgroundConfig - } -} - -private extension MainTimelineCell { - func configure(_ cellData: MainTimelineCellData) { + + private func configure(_ cellData: MainTimelineCellData) { articleContent.numberOfLines = cellData.numberOfLines addArticleContent(configurationState) - - switch cellData.showFeedName { - case .feed: + + if cellData.showFeedName == .feed { articleByLine.text = cellData.feedName - case .byline: + } else if cellData.showFeedName == .byline { articleByLine.text = cellData.byline - case .none: + } else if cellData.showFeedName == .none { articleByLine.text = "" } - - setIconImage(cellData.iconImage, with: cellData.iconSize) - + + if feedIcon != nil { + setIconImage(cellData.iconImage, with: cellData.iconSize) + } + articleDate.text = cellData.dateString } - - func updateIndicatorView(_ state: UICellConfigurationState) { + + private func updateIndicatorView(_ state: UICellConfigurationState) { if cellData.read == false { if indicatorView.alpha == 0.0 { indicatorView.alpha = 1.0 } - UIView.animate(withDuration: Self.indicatorAnimationDuration) { + UIView.animate(withDuration: 0.25) { self.indicatorView.iconImage = Assets.Images.unreadCellIndicator - self.indicatorView.tintColor = self.isActive(state) ? .white : Assets.Colors.secondaryAccent + self.indicatorView.tintColor = (state.isSelected && !state.isSwiped) ? .white : Assets.Colors.secondaryAccent } return - } else if cellData.starred { + } + else if cellData.starred { if indicatorView.alpha == 0.0 { indicatorView.alpha = 1.0 } - UIView.animate(withDuration: Self.indicatorAnimationDuration) { + UIView.animate(withDuration: 0.25) { self.indicatorView.iconImage = Assets.Images.starredFeed - self.indicatorView.tintColor = self.isActive(state) ? .white : Assets.Colors.star + self.indicatorView.tintColor = (state.isSelected && !state.isSwiped) ? .white : Assets.Colors.star } return - } else if indicatorView.alpha == 1.0 { - UIView.animate(withDuration: Self.indicatorAnimationDuration) { + } + else if indicatorView.alpha == 1.0 { + UIView.animate(withDuration: 0.25) { self.indicatorView.alpha = 0.0 self.indicatorView.iconImage = nil } } } - - func configureStackView() { + + private func configureStackView() { switch traitCollection.preferredContentSizeCategory { case .accessibilityMedium, .accessibilityLarge, .accessibilityExtraLarge, .accessibilityExtraExtraLarge, .accessibilityExtraExtraExtraLarge: metaDataStackView.axis = .vertical @@ -128,106 +97,136 @@ private extension MainTimelineCell { metaDataStackView.distribution = .fillEqually } } - - func isActive(_ state: UICellConfigurationState) -> Bool { - state.isSwiped || state.isSelected || state.isEditing || state.isHighlighted - } - - func setIconImage(_ iconImage: IconImage?, with size: IconSize) { - setIconImage(iconImage) - updateIconViewSizeConstraints(to: size.size) - } - - func updateIconViewSizeConstraints(to size: CGSize) { - guard let feedIcon else { - return + + private func setIconImage(_ iconImage: IconImage?, with size: IconSize) { + if feedIcon != nil { + updateIconViewSizeConstraints(to: size.size) + feedIcon!.iconImage = iconImage } - - for constraint in feedIcon.constraints { + } + + private func updateIconViewSizeConstraints(to size: CGSize) { + guard feedIcon != nil else { return } + + for constraint in feedIcon!.constraints.compactMap({ $0 }) { constraint.isActive = false } - + NSLayoutConstraint.activate([ - feedIcon.widthAnchor.constraint(equalToConstant: size.width), - feedIcon.heightAnchor.constraint(equalToConstant: size.height) + feedIcon!.widthAnchor.constraint(equalToConstant: size.width), + feedIcon!.heightAnchor.constraint(equalToConstant: size.height) ]) - + setNeedsLayout() } - - func paragraphStyle(lineHeight: CGFloat) -> NSParagraphStyle { - let paragraphStyle = NSMutableParagraphStyle() - - paragraphStyle.minimumLineHeight = lineHeight - paragraphStyle.maximumLineHeight = lineHeight - paragraphStyle.lineBreakMode = .byWordWrapping - paragraphStyle.lineBreakStrategy = [] - paragraphStyle.hyphenationFactor = 1.0 - paragraphStyle.allowsDefaultTighteningForTruncation = true - - return paragraphStyle - } - - func attributes(textStyle: UIFont.TextStyle, color: UIColor) -> [NSAttributedString.Key: Any] { - let font = UIFont.preferredFont(forTextStyle: textStyle) - let paragraphStyle = paragraphStyle(lineHeight: font.lineHeight) - return [.font: font, .paragraphStyle: paragraphStyle, .foregroundColor: color] - } - - func addArticleContent(_ state: UICellConfigurationState) { + + private func addArticleContent(_ state: UICellConfigurationState) { let attributedCellText = NSMutableAttributedString() - + // 1. Prepare Title - if !cellData.title.isEmpty { - let titleAttributes = attributes(textStyle: .headline, color: titleTextColor(for: state)) + if cellData.title != "" { + let titleFont = UIFont.preferredFont(forTextStyle: .headline) + let lineHeight = titleFont.lineHeight + + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.minimumLineHeight = lineHeight + paragraphStyle.maximumLineHeight = lineHeight + paragraphStyle.lineBreakMode = .byWordWrapping + paragraphStyle.lineBreakStrategy = [] + paragraphStyle.hyphenationFactor = 1.0 + paragraphStyle.allowsDefaultTighteningForTruncation = true + + let titleAttributes: [NSAttributedString.Key: Any] = [ + .font: titleFont, + .paragraphStyle: paragraphStyle, + .foregroundColor: UIColor.label + ] + let titleAttributed = NSAttributedString(string: cellData.title, attributes: titleAttributes) - - // 2. Measure Title Height let linesUsed = countLines(of: titleAttributed, width: articleContent.bounds.width) + // 2. Measure Title Height if linesUsed >= cellData.numberOfLines { - // The title already fills numberOfLines lines, set it and exit + // The title already fills 3 lines, set it and exit articleContent.attributedText = titleAttributed articleContent.lineBreakMode = .byTruncatingTail return } - + attributedCellText.append(titleAttributed) } - - // 3. Prepare Summary (only reached if title < numberOfLines lines) - if !cellData.summary.isEmpty { - let summaryAttributes = attributes(textStyle: .body, color: titleTextColor(for: state)) + + // 3. Prepare Summary (Only reached if Title < 3 lines) + if cellData.summary != "" { + let summaryFont = UIFont.preferredFont(forTextStyle: .body) + let summaryLineHeight = summaryFont.lineHeight + + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.minimumLineHeight = summaryLineHeight + paragraphStyle.maximumLineHeight = summaryLineHeight + paragraphStyle.lineBreakMode = .byWordWrapping + paragraphStyle.lineBreakStrategy = [] + paragraphStyle.hyphenationFactor = 1.0 + paragraphStyle.allowsDefaultTighteningForTruncation = true + + let summaryAttributes: [NSAttributedString.Key: Any] = [ + .font: summaryFont, + .paragraphStyle: paragraphStyle, + .foregroundColor: cellData.title != "" ? UIColor.secondaryLabel : UIColor.label + ] + let prefix = cellData.title != "" ? "\n" : "" let summaryAttributed = NSAttributedString(string: prefix + cellData.summary, attributes: summaryAttributes) attributedCellText.append(summaryAttributed) } - - articleContent.attributedText = attributedCellText - + let linesUsed = countLines(of: attributedCellText, width: articleContent.bounds.width) - let lineBreakMode: NSLineBreakMode = linesUsed >= cellData.numberOfLines ? .byTruncatingTail : .byWordWrapping - articleContent.lineBreakMode = lineBreakMode - } + if linesUsed >= cellData.numberOfLines { + // The title already fills 3 lines, set it and exit + articleContent.attributedText = attributedCellText + articleContent.lineBreakMode = .byTruncatingTail + return + } else { + articleContent.attributedText = attributedCellText + articleContent.lineBreakMode = .byWordWrapping + } - func titleTextColor(for state: UICellConfigurationState) -> UIColor { - isActive(state) ? .white : .label } - + + func adjustArticleContentColor() { + if configurationState.isSwiped && configurationState.isSelected { + articleContent.textColor = .white + articleByLine.textColor = .white + articleDate.textColor = .white + } else if configurationState.isSwiped && !configurationState.isSelected { + addArticleContent(configurationState) + articleByLine.textColor = .secondaryLabel + articleDate.textColor = .secondaryLabel + } else if !configurationState.isSwiped && configurationState.isSelected { + articleContent.textColor = .white + articleByLine.textColor = .white + articleDate.textColor = .white + } else { + addArticleContent(configurationState) + articleByLine.textColor = .secondaryLabel + articleDate.textColor = .secondaryLabel + } + } + func countLines(of attributedString: NSAttributedString, width: CGFloat) -> Int { let textStorage = NSTextStorage(attributedString: attributedString) let textContainer = NSTextContainer(size: CGSize(width: width, height: .greatestFiniteMagnitude)) let layoutManager = NSLayoutManager() - + layoutManager.addTextContainer(textContainer) textStorage.addLayoutManager(layoutManager) - + textContainer.lineFragmentPadding = 0 // UILabel uses 0 or very small layoutManager.ensureLayout(for: textContainer) - + var lineCount = 0 var index = 0 var lineRange = NSRange() - + while index < layoutManager.numberOfGlyphs { layoutManager.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange) index = NSMaxRange(lineRange) @@ -235,4 +234,31 @@ private extension MainTimelineCell { } return lineCount } + + override func updateConfiguration(using state: UICellConfigurationState) { + super.updateConfiguration(using: state) + + var backgroundConfig = UIBackgroundConfiguration.listCell().updated(for: state) + backgroundConfig.cornerRadius = 20 + + if traitCollection.userInterfaceIdiom == .pad { + backgroundConfig.edgesAddingLayoutMarginsToBackgroundInsets = [.leading, .trailing] + backgroundConfig.backgroundInsets = NSDirectionalEdgeInsets(top: 0, leading: !isPreview ? -12 : -12, bottom: 0, trailing: !isPreview ? -12 : -12) + } + + if state.isSwiped && state.isSelected { + backgroundConfig.backgroundColor = Assets.Colors.primaryAccent + } else if state.isSwiped && !state.isSelected { + backgroundConfig.backgroundColor = .secondarySystemFill + } else if !state.isSwiped && state.isSelected { + backgroundConfig.backgroundColor = Assets.Colors.primaryAccent + } else { + backgroundConfig.backgroundColor = .clear + } + adjustArticleContentColor() + updateIndicatorView(state) + + self.backgroundConfiguration = backgroundConfig + } } + diff --git a/iOS/MainTimeline/Cells/MainTimelineCollectionViewCell.swift b/iOS/MainTimeline/Cells/MainTimelineCollectionViewCell.swift index 5af73c332..145152461 100644 --- a/iOS/MainTimeline/Cells/MainTimelineCollectionViewCell.swift +++ b/iOS/MainTimeline/Cells/MainTimelineCollectionViewCell.swift @@ -99,12 +99,6 @@ class MainTimelineCollectionViewCell: UICollectionViewCell { } } - private func isActive(_ state: UICellConfigurationState) -> Bool { - let active = state.isSwiped || state.isSelected || state.isEditing || state.isHighlighted - return active - } - - private func setIconImage(_ iconImage: IconImage?, with size: IconSize) { if feedIcon != nil { updateIconViewSizeConstraints(to: size.size) @@ -146,7 +140,7 @@ class MainTimelineCollectionViewCell: UICollectionViewCell { let titleAttributes: [NSAttributedString.Key: Any] = [ .font: titleFont, .paragraphStyle: paragraphStyle, - .foregroundColor: titleTextColor(for: state) + .foregroundColor: UIColor.label ] let titleAttributed = NSAttributedString(string: cellData.title, attributes: titleAttributes) @@ -178,7 +172,7 @@ class MainTimelineCollectionViewCell: UICollectionViewCell { let summaryAttributes: [NSAttributedString.Key: Any] = [ .font: summaryFont, .paragraphStyle: paragraphStyle, - .foregroundColor: titleTextColor(for: state) + .foregroundColor: cellData.title != "" ? UIColor.secondaryLabel : UIColor.label ] let prefix = cellData.title != "" ? "\n" : "" @@ -199,15 +193,23 @@ class MainTimelineCollectionViewCell: UICollectionViewCell { } - func titleTextColor(for state: UICellConfigurationState) -> UIColor { - if state.isSwiped && state.isSelected { - return .white - } else if state.isSwiped && !state.isSelected { - return .label - } else if !state.isSwiped && state.isSelected { - return .white + func adjustArticleContentColor() { + if configurationState.isSwiped && configurationState.isSelected { + articleContent.textColor = .white + articleByLine.textColor = .white + articleDate.textColor = .white + } else if configurationState.isSwiped && !configurationState.isSelected { + addArticleContent(configurationState) + articleByLine.textColor = .secondaryLabel + articleDate.textColor = .secondaryLabel + } else if !configurationState.isSwiped && configurationState.isSelected { + articleContent.textColor = .white + articleByLine.textColor = .white + articleDate.textColor = .white } else { - return .label + addArticleContent(configurationState) + articleByLine.textColor = .secondaryLabel + articleDate.textColor = .secondaryLabel } } @@ -247,30 +249,18 @@ class MainTimelineCollectionViewCell: UICollectionViewCell { if state.isSwiped && state.isSelected { backgroundConfig.backgroundColor = Assets.Colors.primaryAccent - articleContent.textColor = titleTextColor(for: state) - articleDate.textColor = .lightText - articleByLine.textColor = .lightText topSeparator.alpha = 0.0 } else if state.isSwiped && !state.isSelected { backgroundConfig.backgroundColor = .secondarySystemFill - articleContent.textColor = titleTextColor(for: state) - articleDate.textColor = .secondaryLabel - articleByLine.textColor = .secondaryLabel topSeparator.alpha = 0.0 } else if !state.isSwiped && state.isSelected { backgroundConfig.backgroundColor = Assets.Colors.primaryAccent - articleContent.textColor = titleTextColor(for: state) - articleDate.textColor = .lightText - articleByLine.textColor = .lightText topSeparator.alpha = 0.0 } else { backgroundConfig.backgroundColor = .clear - articleContent.textColor = titleTextColor(for: state) - articleDate.textColor = .secondaryLabel - articleByLine.textColor = .secondaryLabel topSeparator.alpha = 1.0 } - + adjustArticleContentColor() updateIndicatorView(state) self.backgroundConfiguration = backgroundConfig