Created
May 23, 2020 22:32
-
-
Save pilot34/6e88560f30ef96a6c4c083ef7e22b6a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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