Created
January 11, 2018 16:19
-
-
Save samskiter/414b6410f1171bca82596673f1eeacb5 to your computer and use it in GitHub Desktop.
A playground demonstrating an issue with UILabel's intrinsic content size
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
import UIKit | |
import PlaygroundSupport | |
class LabelView: UIView { | |
let label = UILabel() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.setup() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
private func setup() { | |
self.label.textColor = .black | |
self.label.backgroundColor = .green | |
self.backgroundColor = .red | |
self.label.numberOfLines = 0 | |
self.addSubview(self.label) | |
self.label.translatesAutoresizingMaskIntoConstraints = false | |
self.label.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true | |
self.label.trailingAnchor.constraint(lessThanOrEqualTo: self.trailingAnchor).isActive = true | |
self.label.topAnchor.constraint(equalTo: self.topAnchor).isActive = true | |
self.label.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true | |
} | |
override var intrinsicContentSize: CGSize { | |
let size = self.label.intrinsicContentSize | |
print(size) | |
return size | |
} | |
} | |
class CustomView: UIView { | |
let container = UIView() | |
let leftLabel = LabelView() | |
let rightLabel = LabelView() | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.setUp() | |
} | |
private func setUp() { | |
self.backgroundColor = .white | |
self.container.backgroundColor = .gray | |
self.addSubview(self.container) | |
self.leftLabel.label.text = "Hello" | |
self.container.addSubview(self.leftLabel) | |
self.rightLabel.label.text = "Hello" | |
self.rightLabel.backgroundColor = .purple | |
self.container.addSubview(self.rightLabel) | |
self.container.translatesAutoresizingMaskIntoConstraints = false | |
self.container.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true | |
self.container.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true | |
self.container.widthAnchor.constraint(equalTo: self.widthAnchor, constant: -20).isActive = true | |
self.leftLabel.translatesAutoresizingMaskIntoConstraints = false | |
self.leftLabel.leftAnchor.constraint(equalTo: self.container.leftAnchor, constant: 10).isActive = true | |
self.leftLabel.topAnchor.constraint(greaterThanOrEqualTo: self.container.topAnchor, constant: 10).isActive = true | |
self.leftLabel.bottomAnchor.constraint(lessThanOrEqualTo: self.container.bottomAnchor, constant: -10).isActive = true | |
self.rightLabel.translatesAutoresizingMaskIntoConstraints = false | |
self.rightLabel.rightAnchor.constraint(equalTo: self.container.rightAnchor, constant: -10).isActive = true | |
self.rightLabel.topAnchor.constraint(greaterThanOrEqualTo: self.container.topAnchor, constant: 10).isActive = true | |
self.rightLabel.bottomAnchor.constraint(lessThanOrEqualTo: self.container.bottomAnchor, constant: -10).isActive = true | |
self.leftLabel.rightAnchor.constraint(equalTo: self.rightLabel.leftAnchor, constant: -10).isActive = true | |
#if xcode8 | |
// Allow the left size to grow with a low hugging priority | |
self.leftLabel.setContentHuggingPriority(UILayoutPriorityDefaultLow, for: .horizontal) | |
// Require the right size to be no bigger than its intrinsic content size | |
self.rightLabel.setContentHuggingPriority(UILayoutPriorityRequired, for: .horizontal) | |
// Allow the left side to be compressed smaller than its intrinsic content size | |
self.leftLabel.setContentCompressionResistancePriority(UILayoutPriorityDefaultHigh, for: .horizontal) | |
// Require the right side to be no smaller than its intrinsic content size | |
self.rightLabel.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .horizontal) | |
#else | |
// Allow the left size to grow with a low hugging priority | |
self.leftLabel.setContentHuggingPriority(.defaultLow, for: .horizontal) | |
// Require the right size to be no bigger than its intrinsic content size | |
self.rightLabel.setContentHuggingPriority(.required, for: .horizontal) | |
// Allow the left side to be compressed smaller than its intrinsic content size | |
self.leftLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal) | |
// Require the right side to be no smaller than its intrinsic content size | |
self.rightLabel.setContentCompressionResistancePriority(.required, for: .horizontal) | |
#endif | |
} | |
} | |
class ViewController: UIViewController { | |
let customView = CustomView() | |
override func loadView() { | |
self.view = self.customView | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
} | |
print(UIDevice.current.systemVersion) | |
let controller = ViewController() | |
PlaygroundPage.current.liveView = UINavigationController(rootViewController: controller) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment