168 lines
6.3 KiB
Swift
168 lines
6.3 KiB
Swift
//
|
|
// AutoCompleteTableViewCell.swift
|
|
// Mastodon
|
|
//
|
|
// Created by MainasuK Cirno on 2021-5-17.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
final class AutoCompleteTableViewCell: UITableViewCell {
|
|
|
|
static let avatarImageSize = CGSize(width: 42, height: 42)
|
|
static let avatarImageCornerRadius: CGFloat = 4
|
|
static let avatarToLabelSpacing: CGFloat = 12
|
|
|
|
let containerStackView: UIStackView = {
|
|
let stackView = UIStackView()
|
|
stackView.axis = .horizontal
|
|
stackView.spacing = 12
|
|
return stackView
|
|
}()
|
|
|
|
let contentStackView: UIStackView = {
|
|
let stackView = UIStackView()
|
|
stackView.axis = .vertical
|
|
stackView.distribution = .fillEqually
|
|
return stackView
|
|
}()
|
|
|
|
let avatarImageView = UIImageView()
|
|
|
|
let titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold), maximumPointSize: 22)
|
|
label.textColor = Asset.Colors.brandBlue.color
|
|
label.text = "Title"
|
|
return label
|
|
}()
|
|
|
|
let subtitleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = UIFontMetrics(forTextStyle: .subheadline).scaledFont(for: .systemFont(ofSize: 15, weight: .regular), maximumPointSize: 20)
|
|
label.textColor = Asset.Colors.Label.secondary.color
|
|
label.text = "subtitle"
|
|
return label
|
|
}()
|
|
|
|
let separatorLine = UIView.separatorLine
|
|
|
|
override func prepareForReuse() {
|
|
super.prepareForReuse()
|
|
avatarImageView.af.cancelImageRequest()
|
|
avatarImageView.kf.cancelDownloadTask()
|
|
}
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
_init()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
_init()
|
|
}
|
|
|
|
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
|
|
super.setHighlighted(highlighted, animated: animated)
|
|
|
|
// workaround for hitTest trigger highlighted issue
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
if self.isHighlighted {
|
|
self.setHighlighted(false, animated: true)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension AutoCompleteTableViewCell {
|
|
|
|
private func _init() {
|
|
backgroundColor = .clear
|
|
|
|
let topPaddingView = UIView()
|
|
let bottomPaddingView = UIView()
|
|
|
|
topPaddingView.translatesAutoresizingMaskIntoConstraints = false
|
|
contentView.addSubview(topPaddingView)
|
|
NSLayoutConstraint.activate([
|
|
topPaddingView.topAnchor.constraint(equalTo: contentView.topAnchor),
|
|
topPaddingView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
|
topPaddingView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
|
|
topPaddingView.heightAnchor.constraint(equalToConstant: 12).priority(.defaultHigh),
|
|
])
|
|
|
|
containerStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
contentView.addSubview(containerStackView)
|
|
NSLayoutConstraint.activate([
|
|
containerStackView.topAnchor.constraint(equalTo: topPaddingView.bottomAnchor),
|
|
containerStackView.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor),
|
|
containerStackView.trailingAnchor.constraint(equalTo: contentView.readableContentGuide.trailingAnchor),
|
|
])
|
|
|
|
avatarImageView.translatesAutoresizingMaskIntoConstraints = false
|
|
containerStackView.addArrangedSubview(avatarImageView)
|
|
NSLayoutConstraint.activate([
|
|
avatarImageView.heightAnchor.constraint(equalToConstant: AutoCompleteTableViewCell.avatarImageSize.height).priority(.required - 1),
|
|
avatarImageView.widthAnchor.constraint(equalToConstant: AutoCompleteTableViewCell.avatarImageSize.width).priority(.required - 1),
|
|
])
|
|
containerStackView.addArrangedSubview(contentStackView)
|
|
contentStackView.addArrangedSubview(titleLabel)
|
|
contentStackView.addArrangedSubview(subtitleLabel)
|
|
|
|
bottomPaddingView.translatesAutoresizingMaskIntoConstraints = false
|
|
contentView.addSubview(bottomPaddingView)
|
|
NSLayoutConstraint.activate([
|
|
bottomPaddingView.topAnchor.constraint(equalTo: contentStackView.bottomAnchor),
|
|
bottomPaddingView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
|
bottomPaddingView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
|
|
bottomPaddingView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
|
|
bottomPaddingView.heightAnchor.constraint(equalTo: topPaddingView.heightAnchor, multiplier: 1.0),
|
|
])
|
|
|
|
separatorLine.translatesAutoresizingMaskIntoConstraints = false
|
|
contentView.addSubview(separatorLine)
|
|
NSLayoutConstraint.activate([
|
|
separatorLine.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
|
separatorLine.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
|
|
separatorLine.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
|
|
separatorLine.heightAnchor.constraint(equalToConstant: UIView.separatorLineHeight(of: contentView)).priority(.defaultHigh),
|
|
])
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - AvatarConfigurableView
|
|
extension AutoCompleteTableViewCell: AvatarConfigurableView {
|
|
static var configurableAvatarImageSize: CGSize { avatarImageSize }
|
|
static var configurableAvatarImageCornerRadius: CGFloat { avatarImageCornerRadius }
|
|
var configurableAvatarImageView: UIImageView? { avatarImageView }
|
|
var configurableAvatarButton: UIButton? { nil }
|
|
}
|
|
|
|
#if canImport(SwiftUI) && DEBUG
|
|
import SwiftUI
|
|
|
|
struct AutoCompleteTableViewCell_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
Group {
|
|
UIViewPreview() {
|
|
let cell = AutoCompleteTableViewCell()
|
|
return cell
|
|
}
|
|
.previewLayout(.fixed(width: 375, height: 66))
|
|
UIViewPreview() {
|
|
let cell = AutoCompleteTableViewCell()
|
|
return cell
|
|
}
|
|
.preferredColorScheme(.dark)
|
|
.previewLayout(.fixed(width: 375, height: 66))
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|