metatext/Views/UIKit/PollOptionButton.swift

53 lines
1.9 KiB
Swift
Raw Normal View History

2020-10-25 02:31:44 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Mastodon
import UIKit
2020-11-09 06:22:20 +00:00
final class PollOptionButton: UIButton {
2021-01-12 07:33:35 +00:00
init(title: String, emojis: [Emoji], multipleSelection: Bool) {
2020-10-25 02:31:44 +00:00
super.init(frame: .zero)
titleLabel?.font = .preferredFont(forTextStyle: .callout)
titleLabel?.adjustsFontForContentSizeCategory = true
titleLabel?.numberOfLines = 0
titleLabel?.lineBreakMode = .byWordWrapping
contentHorizontalAlignment = .leading
titleEdgeInsets = Self.titleEdgeInsets
let attributedTitle = NSMutableAttributedString(string: title)
2021-01-12 07:33:35 +00:00
attributedTitle.insert(emojis: emojis, view: titleLabel!)
2020-10-25 02:31:44 +00:00
attributedTitle.resizeAttachments(toLineHeight: titleLabel!.font.lineHeight)
setAttributedTitle(attributedTitle, for: .normal)
setImage(
UIImage(
systemName: multipleSelection ? "square" : "circle",
withConfiguration: UIImage.SymbolConfiguration(scale: .medium)),
for: .normal)
setImage(
UIImage(
systemName: multipleSelection ? "checkmark.square" : "checkmark.circle",
withConfiguration: UIImage.SymbolConfiguration(scale: .medium)),
for: .selected)
setContentCompressionResistancePriority(.required, for: .vertical)
heightAnchor.constraint(equalTo: titleLabel!.heightAnchor).isActive = true
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
2021-01-19 00:46:38 +00:00
extension PollOptionButton {
static func estimatedHeight(width: CGFloat, title: String) -> CGFloat {
title.height(width: width, font: .preferredFont(forTextStyle: .callout))
}
}
2020-10-25 02:31:44 +00:00
private extension PollOptionButton {
2021-01-28 06:36:46 +00:00
static let titleEdgeInsets = UIEdgeInsets(top: 0, left: .defaultSpacing, bottom: 0, right: .compactSpacing)
2020-10-25 02:31:44 +00:00
}