From 423bdb24735b75bb68817a8e58c17b1b9d678799 Mon Sep 17 00:00:00 2001 From: CMK Date: Thu, 27 May 2021 14:57:20 +0800 Subject: [PATCH] feat: support display custom emoji for field name --- .../Section/ProfileFieldSection.swift | 27 ++++++++++++------- Mastodon/Extension/ActiveLabel.swift | 9 +++++-- ...ofileFieldAddEntryCollectionViewCell.swift | 7 ++++- .../Header/View/ProfileFieldView.swift | 17 +++++++++++- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/Mastodon/Diffiable/Section/ProfileFieldSection.swift b/Mastodon/Diffiable/Section/ProfileFieldSection.swift index 41334ba3f..0b67b00a7 100644 --- a/Mastodon/Diffiable/Section/ProfileFieldSection.swift +++ b/Mastodon/Diffiable/Section/ProfileFieldSection.swift @@ -33,18 +33,24 @@ extension ProfileFieldSection { cell.separatorLineToMarginLeadingLayoutConstraint.constant = margin // set key + cell.fieldView.titleActiveLabel.configure(field: field.name.value, emojiDict: attribute.emojiDict.value) cell.fieldView.titleTextField.text = field.name.value - field.name - .removeDuplicates() - .receive(on: RunLoop.main) - .sink { [weak cell] name in - guard let cell = cell else { return } - cell.fieldView.titleTextField.text = name - } - .store(in: &cell.disposeBag) + Publishers.CombineLatest( + field.name.removeDuplicates(), + attribute.emojiDict.removeDuplicates() + ) + .receive(on: RunLoop.main) + .sink { [weak cell] name, emojiDict in + guard let cell = cell else { return } + cell.fieldView.titleActiveLabel.configure(field: name, emojiDict: emojiDict) + cell.fieldView.titleTextField.text = name + } + .store(in: &cell.disposeBag) + // set value cell.fieldView.valueActiveLabel.configure(field: field.value.value, emojiDict: attribute.emojiDict.value) + cell.fieldView.valueTextField.text = field.value.value Publishers.CombineLatest( field.value.removeDuplicates(), attribute.emojiDict.removeDuplicates() @@ -72,9 +78,10 @@ extension ProfileFieldSection { } // setup editing state - cell.fieldView.titleTextField.isEnabled = attribute.isEditing - cell.fieldView.valueActiveLabel.isHidden = attribute.isEditing + cell.fieldView.titleTextField.isHidden = !attribute.isEditing cell.fieldView.valueTextField.isHidden = !attribute.isEditing + cell.fieldView.titleActiveLabel.isHidden = attribute.isEditing + cell.fieldView.valueActiveLabel.isHidden = attribute.isEditing // set control hidden let isHidden = !attribute.isEditing diff --git a/Mastodon/Extension/ActiveLabel.swift b/Mastodon/Extension/ActiveLabel.swift index 7975fad57..2c32bc4fc 100644 --- a/Mastodon/Extension/ActiveLabel.swift +++ b/Mastodon/Extension/ActiveLabel.swift @@ -16,7 +16,8 @@ extension ActiveLabel { case `default` case statusHeader case statusName - case profileField + case profileFieldName + case profileFieldValue } convenience init(style: Style) { @@ -46,7 +47,11 @@ extension ActiveLabel { font = .systemFont(ofSize: 17, weight: .semibold) textColor = Asset.Colors.Label.primary.color numberOfLines = 1 - case .profileField: + case .profileFieldName: + font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold), maximumPointSize: 20) + textColor = Asset.Colors.Label.primary.color + numberOfLines = 1 + case .profileFieldValue: font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .regular), maximumPointSize: 20) textColor = Asset.Colors.Label.primary.color numberOfLines = 1 diff --git a/Mastodon/Scene/Profile/Header/View/ProfileFieldAddEntryCollectionViewCell.swift b/Mastodon/Scene/Profile/Header/View/ProfileFieldAddEntryCollectionViewCell.swift index c44bac6ce..1801d4222 100644 --- a/Mastodon/Scene/Profile/Header/View/ProfileFieldAddEntryCollectionViewCell.swift +++ b/Mastodon/Scene/Profile/Header/View/ProfileFieldAddEntryCollectionViewCell.swift @@ -96,8 +96,13 @@ extension ProfileFieldAddEntryCollectionViewCell { bottomSeparatorLine.heightAnchor.constraint(equalToConstant: UIView.separatorLineHeight(of: self)).priority(.defaultHigh), ]) - fieldView.titleTextField.text = L10n.Scene.Profile.Fields.addRow + fieldView.titleActiveLabel.isHidden = false + fieldView.titleActiveLabel.configure(field: L10n.Scene.Profile.Fields.addRow, emojiDict: [:]) + fieldView.titleTextField.isHidden = true + + fieldView.valueActiveLabel.isHidden = false fieldView.valueActiveLabel.configure(field: " ", emojiDict: [:]) + fieldView.valueTextField.isHidden = true addGestureRecognizer(singleTagGestureRecognizer) singleTagGestureRecognizer.addTarget(self, action: #selector(ProfileFieldAddEntryCollectionViewCell.singleTapGestureRecognizerHandler(_:))) diff --git a/Mastodon/Scene/Profile/Header/View/ProfileFieldView.swift b/Mastodon/Scene/Profile/Header/View/ProfileFieldView.swift index 1900a00f9..14e5e4b33 100644 --- a/Mastodon/Scene/Profile/Header/View/ProfileFieldView.swift +++ b/Mastodon/Scene/Profile/Header/View/ProfileFieldView.swift @@ -17,6 +17,14 @@ final class ProfileFieldView: UIView { let name = PassthroughSubject() let value = PassthroughSubject() + // for custom emoji display + let titleActiveLabel: ActiveLabel = { + let label = ActiveLabel(style: .profileFieldName) + label.configure(content: "title", emojiDict: [:]) + return label + }() + + // for editing let titleTextField: UITextField = { let textField = UITextField() textField.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold), maximumPointSize: 20) @@ -28,7 +36,7 @@ final class ProfileFieldView: UIView { // for custom emoji display let valueActiveLabel: ActiveLabel = { - let label = ActiveLabel(style: .profileField) + let label = ActiveLabel(style: .profileFieldValue) label.configure(content: "value", emojiDict: [:]) return label }() @@ -73,6 +81,12 @@ extension ProfileFieldView { containerStackView.trailingAnchor.constraint(equalTo: trailingAnchor), containerStackView.bottomAnchor.constraint(equalTo: bottomAnchor), ]) + titleActiveLabel.translatesAutoresizingMaskIntoConstraints = false + containerStackView.addArrangedSubview(titleActiveLabel) + NSLayoutConstraint.activate([ + titleActiveLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 44).priority(.defaultHigh), + ]) + titleTextField.setContentHuggingPriority(.defaultLow - 1, for: .horizontal) titleTextField.translatesAutoresizingMaskIntoConstraints = false containerStackView.addArrangedSubview(titleTextField) NSLayoutConstraint.activate([ @@ -92,6 +106,7 @@ extension ProfileFieldView { valueTextField.widthAnchor.constraint(greaterThanOrEqualToConstant: 44).priority(.defaultHigh), ]) + titleTextField.isHidden = true valueTextField.isHidden = true NotificationCenter.default