That is my AttributedLabel class:
import UIKit
class AttributedLabel: UILabel {
var cornerRadius: CGFloat = 3 {
didSet {
layer.cornerRadius = cornerRadius
}
}
var borderColor: UIColor = .clear {
didSet {
layer.borderColor = borderColor.cgColor
layer.borderWidth = 1
}
}
var masksToBounds: Bool = false {
didSet {
layer.masksToBounds = masksToBounds
}
}
var borderWidth: CGFloat = 1 {
didSet {
layer.borderWidth = borderWidth
}
}
personal var strikeThrough = false
personal var paddingTop: CGFloat = 0
personal var paddingBottom: CGFloat = 0
personal var paddingLeft: CGFloat = 0
personal var paddingRight: CGFloat = 0
override var textual content: String? {
didSet {
if strikeThrough {
attributedText = NSAttributedString(string: textual content ?? "", strikeThrough: true)
}
}
}
// MARK: - Initialization
comfort init(
paddingTop: CGFloat = 0,
paddingBottom: CGFloat = 0,
paddingLeft: CGFloat = 0,
paddingRight: CGFloat = 0,
strikeThrough: Bool = false
) {
self.init()
self.paddingTop = paddingTop
self.paddingBottom = paddingBottom
self.paddingLeft = paddingLeft
self.paddingRight = paddingRight
self.strikeThrough = strikeThrough
backgroundColor = .yellow
}
// MARK: - Overriden
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(prime: paddingTop, left: paddingLeft, backside: paddingBottom, proper: paddingRight)
tremendous.drawText(in: rect.inset(by: insets))
}
override var intrinsicContentSize: CGSize {
var contentSize = tremendous.intrinsicContentSize
contentSize.peak += paddingTop
contentSize.peak += paddingBottom
contentSize.width += paddingLeft
contentSize.width += paddingRight
return contentSize
}
}
And that is the outcome:
First label is truncated. Why? It’s outlined like this:
personal let titleLabel: UILabel = {
let label = AttributedLabel(paddingLeft: 20, paddingRight: 20)
label.textColor = .mineShaft
label.font = UIFont.poppinsMedium.withSize(18)
label.textAlignment = .left
label.numberOfLines = 0
return label
}()
and has textual content assigned:
titleLabel.textual content = "Czy jesteś za wdrożeniem aplikacji PLZ?"
I dont know if it nmatters however titleLabel is added into UIStackView:
contentStackView.addArrangedSubview(titleLabel)