metatext/Views/UIKit/CapsuleLabel.swift

54 lines
1.5 KiB
Swift
Raw Normal View History

2021-01-31 18:43:37 +00:00
// Copyright © 2021 Metabolist. All rights reserved.
import UIKit
2021-02-11 02:04:04 +00:00
final class CapsuleLabel: UILabel {
2021-01-31 18:43:37 +00:00
override init(frame: CGRect) {
super.init(frame: frame)
initialSetup()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
2021-03-05 20:36:49 +00:00
layer.cornerRadius = bounds.height / 2
2021-01-31 18:43:37 +00:00
invalidateIntrinsicContentSize()
}
override func drawText(in rect: CGRect) {
2021-03-05 20:36:49 +00:00
super.drawText(in: rect.inset(by: .init(
top: .compactSpacing,
left: .defaultSpacing,
bottom: .compactSpacing,
right: .defaultSpacing)))
2021-01-31 18:43:37 +00:00
}
override var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
2021-03-05 20:36:49 +00:00
size.width += .defaultSpacing * 2
size.height += .compactSpacing * 2
2021-01-31 18:43:37 +00:00
return size
}
}
2021-02-11 02:04:04 +00:00
private extension CapsuleLabel {
2021-01-31 18:43:37 +00:00
func initialSetup() {
2021-02-12 04:46:39 +00:00
backgroundColor = .secondarySystemBackground
2021-01-31 18:43:37 +00:00
textColor = .secondaryLabel
font = UIFont.preferredFont(forTextStyle: .footnote)
adjustsFontForContentSizeCategory = true
setContentHuggingPriority(.required, for: .horizontal)
setContentCompressionResistancePriority(.required, for: .horizontal)
clipsToBounds = true
}
}