Skip to content

Instantly share code, notes, and snippets.

@pilot34
Created May 23, 2020 22:32
Show Gist options
  • Save pilot34/6e88560f30ef96a6c4c083ef7e22b6a9 to your computer and use it in GitHub Desktop.
Save pilot34/6e88560f30ef96a6c4c083ef7e22b6a9 to your computer and use it in GitHub Desktop.
class MyView: UILabel {
init() {
super.init(frame: .zero)
numberOfLines = 0
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
// do nothing, we are not a label actually
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let size = bounds.size
let resultSize = performLayout(changeFrames: false, width: size.width)
return CGRect(origin: .zero, size: resultSize)
}
override func layoutSubviews() {
let width = bounds.size.width
_ = performLayout(changeFrames: true, width: width)
super.layoutSubviews()
}
private func performLayout(changeFrames: Bool, width: CGFloat?) -> CGSize {
// calculate new size here depends on width
// change subviews frames if changeFrames == true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment