override open func draw(_ rect: CGRect) {
drawButton()
super.draw(rect)
}
private func drawButton() {
if #available(iOS 15, *) {
titleLabel?.lineBreakMode = .byTruncatingTail
titleLabel?.numberOfLines = 1
if let text = titleLabel?.text {
let textSize = sizeText(string: text)
let imageSize = imageView?.bounds.size ?? CGSize.zero
let expectedWidth = textSize.width + textGap + imageSize.width + 8
var currentBtnSize = self.bounds.size
currentBtnSize = CGSize(width: max(expectedWidth, currentBtnSize.width), height: currentBtnSize.height)
self.bounds.size = currentBtnSize
setNeedsUpdateConstraints()
}
} else {
contentHorizontalAlignment = .left
titleLabel?.adjustsFontSizeToFitWidth = false
titleLabel?.lineBreakMode = .byTruncatingTail
let imageSize = image(for: .normal)?.size ?? .zero
let optSize = intrinsicContentSize
#warning("consider changing this to mirrow above bounds code")
if bounds.width < optSize.width || bounds.height < optSize.height {
setNeedsLayout()
}
let actualTextWidth = optSize.width - textGap - imageSize.width - trailingGap
imageEdgeInsets = UIEdgeInsets(top: 0.0, left: (actualTextWidth + textGap), bottom: 0.0, right: trailingGap)
titleEdgeInsets = UIEdgeInsets(top: 0.0, left: -imageSize.width, bottom: 0.0, right: -actualTextWidth)
}
}