metatext-app-ios-iphone-ipad/Views/UIKit/CapsuleLabel.swift

50 lines
1.2 KiB
Swift
Raw Normal View History

2021-01-31 19:43:37 +01:00
// Copyright © 2021 Metabolist. All rights reserved.
import UIKit
2021-02-11 03:04:04 +01:00
final class CapsuleLabel: UILabel {
2021-01-31 19:43:37 +01: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()
layer.cornerRadius = inset
invalidateIntrinsicContentSize()
}
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: .init(top: 0, left: inset, bottom: 0, right: inset)))
}
override var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
size.width += inset * 2
return size
}
}
2021-02-11 03:04:04 +01:00
private extension CapsuleLabel {
2021-01-31 19:43:37 +01:00
var inset: CGFloat { bounds.height / 2 }
func initialSetup() {
2021-02-12 05:46:39 +01:00
backgroundColor = .secondarySystemBackground
2021-01-31 19:43:37 +01:00
textColor = .secondaryLabel
font = UIFont.preferredFont(forTextStyle: .footnote)
adjustsFontForContentSizeCategory = true
setContentHuggingPriority(.required, for: .horizontal)
setContentCompressionResistancePriority(.required, for: .horizontal)
clipsToBounds = true
}
}