metatext/Views/UIKit/AccountFieldView.swift

154 lines
6.6 KiB
Swift
Raw Normal View History

2020-11-10 06:27:08 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Mastodon
import UIKit
final class AccountFieldView: UIView {
let nameLabel = UILabel()
let valueTextView = TouchFallthroughTextView()
// swiftlint:disable:next function_body_length
2021-01-12 07:33:35 +00:00
init(name: String, value: NSAttributedString, verifiedAt: Date?, emojis: [Emoji]) {
2020-11-10 06:27:08 +00:00
super.init(frame: .zero)
backgroundColor = .systemBackground
let nameBackgroundView = UIView()
addSubview(nameBackgroundView)
nameBackgroundView.translatesAutoresizingMaskIntoConstraints = false
nameBackgroundView.backgroundColor = .secondarySystemBackground
let valueBackgroundView = UIView()
addSubview(valueBackgroundView)
valueBackgroundView.translatesAutoresizingMaskIntoConstraints = false
valueBackgroundView.backgroundColor = verifiedAt == nil
? .systemBackground
: UIColor.systemGreen.withAlphaComponent(0.25)
2020-11-10 06:27:08 +00:00
addSubview(nameLabel)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.numberOfLines = 0
nameLabel.font = .preferredFont(forTextStyle: .headline)
nameLabel.textAlignment = .center
nameLabel.textColor = .secondaryLabel
let mutableName = NSMutableAttributedString(string: name)
2020-11-10 06:27:08 +00:00
2021-01-12 07:33:35 +00:00
mutableName.insert(emojis: emojis, view: nameLabel)
2020-11-10 06:27:08 +00:00
mutableName.resizeAttachments(toLineHeight: nameLabel.font.lineHeight)
nameLabel.attributedText = mutableName
let dividerView = UIView()
addSubview(dividerView)
dividerView.translatesAutoresizingMaskIntoConstraints = false
dividerView.backgroundColor = .separator
addSubview(valueTextView)
valueTextView.translatesAutoresizingMaskIntoConstraints = false
valueTextView.backgroundColor = .clear
if verifiedAt != nil {
2020-11-10 06:27:08 +00:00
valueTextView.linkTextAttributes = [
.foregroundColor: UIColor.systemGreen as Any,
.underlineColor: UIColor.clear]
}
let valueFont = UIFont.preferredFont(forTextStyle: verifiedAt == nil ? .body : .headline)
let mutableValue = NSMutableAttributedString(attributedString: value)
2020-11-10 06:27:08 +00:00
let valueRange = NSRange(location: 0, length: mutableValue.length)
mutableValue.removeAttribute(.font, range: valueRange)
mutableValue.addAttributes(
[.font: valueFont as Any,
.foregroundColor: UIColor.label],
range: valueRange)
2021-01-12 07:33:35 +00:00
mutableValue.insert(emojis: emojis, view: valueTextView)
2020-11-10 06:27:08 +00:00
mutableValue.resizeAttachments(toLineHeight: valueFont.lineHeight)
valueTextView.attributedText = mutableValue
valueTextView.textAlignment = .center
let checkButton = UIButton()
checkButton.setImage(
UIImage(
systemName: "checkmark",
withConfiguration: UIImage.SymbolConfiguration(scale: .small)),
for: .normal)
addSubview(checkButton)
checkButton.translatesAutoresizingMaskIntoConstraints = false
checkButton.tintColor = .systemGreen
checkButton.isHidden = verifiedAt == nil
2020-11-10 06:27:08 +00:00
checkButton.showsMenuAsPrimaryAction = true
2021-01-28 06:36:46 +00:00
checkButton.setContentCompressionResistancePriority(.required, for: .horizontal)
checkButton.setContentHuggingPriority(.required, for: .horizontal)
2020-11-10 06:27:08 +00:00
if let verifiedAt = verifiedAt {
2020-11-10 06:27:08 +00:00
checkButton.menu = UIMenu(
title: String.localizedStringWithFormat(
NSLocalizedString("account.field.verified", comment: ""),
Self.dateFormatter.string(from: verifiedAt)),
options: .displayInline,
children: [UIAction(title: NSLocalizedString("ok", comment: "")) { _ in }])
}
let nameLabelBottomConstraint = nameLabel.bottomAnchor.constraint(
equalTo: bottomAnchor,
constant: -.defaultSpacing)
let valueTextViewBottomConstraint = valueTextView.bottomAnchor.constraint(
lessThanOrEqualTo: bottomAnchor,
constant: -.defaultSpacing)
for constraint in [nameLabelBottomConstraint, valueTextViewBottomConstraint] {
constraint.priority = .justBelowMax
}
NSLayoutConstraint.activate([
nameLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: .defaultSpacing),
nameLabel.topAnchor.constraint(equalTo: topAnchor, constant: .defaultSpacing),
nameLabelBottomConstraint,
dividerView.leadingAnchor.constraint(equalTo: nameLabel.trailingAnchor, constant: .defaultSpacing),
dividerView.topAnchor.constraint(equalTo: topAnchor),
dividerView.bottomAnchor.constraint(equalTo: bottomAnchor),
dividerView.widthAnchor.constraint(equalToConstant: .hairline),
checkButton.leadingAnchor.constraint(equalTo: dividerView.trailingAnchor, constant: .defaultSpacing),
valueTextView.leadingAnchor.constraint(
equalTo: verifiedAt == nil ? dividerView.trailingAnchor : checkButton.trailingAnchor,
2020-11-10 06:27:08 +00:00
constant: .defaultSpacing),
valueTextView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: .defaultSpacing),
valueTextView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -.defaultSpacing),
valueTextViewBottomConstraint,
nameLabel.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1 / 3),
checkButton.centerYAnchor.constraint(equalTo: valueTextView.centerYAnchor),
valueTextView.centerYAnchor.constraint(equalTo: nameLabel.centerYAnchor),
nameBackgroundView.leadingAnchor.constraint(equalTo: leadingAnchor),
nameBackgroundView.topAnchor.constraint(equalTo: topAnchor),
nameBackgroundView.trailingAnchor.constraint(equalTo: dividerView.leadingAnchor),
nameBackgroundView.bottomAnchor.constraint(equalTo: bottomAnchor),
valueBackgroundView.leadingAnchor.constraint(equalTo: dividerView.trailingAnchor),
valueBackgroundView.topAnchor.constraint(equalTo: topAnchor),
valueBackgroundView.trailingAnchor.constraint(equalTo: trailingAnchor),
valueBackgroundView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private extension AccountFieldView {
static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .full
return formatter
}()
}