metatext-app-ios-iphone-ipad/Views/PollOptionButton.swift

47 lines
1.7 KiB
Swift
Raw Normal View History

2020-10-25 03:31:44 +01:00
// Copyright © 2020 Metabolist. All rights reserved.
import Mastodon
import UIKit
2020-11-09 07:22:20 +01:00
final class PollOptionButton: UIButton {
2020-10-25 03:31:44 +01:00
init(title: String, emoji: [Emoji], multipleSelection: Bool) {
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)
attributedTitle.insert(emoji: emoji, view: titleLabel!)
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")
}
}
private extension PollOptionButton {
static let titleEdgeInsets = UIEdgeInsets(top: 0, left: .compactSpacing, bottom: 0, right: .compactSpacing)
}