We are facing some weird issue in the UILabel initialisation. Also it is occurring sometimes.
class TextLabel: ConfigurableView<TextLabel.Config> {
struct Config {
var text: String = .empty
var font: UIFont?
var textColor: UIColor?
var maxLines: Int = 0
var attributedText: NSAttributedString?
var textAlignment: NSTextAlignment = .natural
var truncateWithMore: Bool = false
var onTapShowMore: (() -> Void)?
var onTap: (() -> Void)?
var accessibilityIdentifier: String?
}
private lazy var label: UILabel = {
let label = UILabel() **//##### Crash is occurring in this line.**
label.translatesAutoresizingMaskIntoConstraints = false
label.adjustsFontForContentSizeCategory = true
return label
}()
private lazy var tapGesture = UITapGestureRecognizer(target: self, action: #selector(onTap))
private var isTruncated = false
override func setUp() {
addSubview(label)
label.equalsContainer()
}
override func layoutSubviews() {
super.layoutSubviews()
updateContent()
}
override func setConfig(_ config: Config) {
super.setConfig(config)
updateContent()
}
@objc func onTap() {
if isTruncated {
config?.onTapShowMore?()
} else {
config?.onTap?()
}
}
func updateContent() {
guard let config = config else { return }
label.numberOfLines = config.maxLines
label.text = config.text
}
}
You can find my configurable view below.
import UIKit
class ConfigurableView<T>: UIControl {
private(set) var config: T?
init(_ config: T) {
super.init(frame: .zero)
setUp()
setConfig(config)
}
override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setUp() {
}
func setConfig(_ config: T) {
self.config = config
}
}
This crash is occurring randomly. Sometimes we could reproduce it in the app updates.
Topic:
UI Frameworks
SubTopic:
UIKit