Tunnel detail: Dynamic Type support

This commit is contained in:
Roopesh Chander 2018-12-09 23:20:25 +05:30
parent 527c1fcb37
commit 3e27a9ddb0
2 changed files with 57 additions and 6 deletions

View File

@ -13,7 +13,7 @@ class ScrollableLabel: UIScrollView {
set(value) { label.textColor = value } set(value) { label.textColor = value }
} }
private let label: UILabel let label: UILabel
init() { init() {
let label = UILabel() let label = UILabel()

View File

@ -37,7 +37,8 @@ class TunnelDetailTableViewController: UITableViewController {
self.title = tunnelViewModel.interfaceData[.name] self.title = tunnelViewModel.interfaceData[.name]
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(editTapped)) self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(editTapped))
self.tableView.rowHeight = 44 self.tableView.estimatedRowHeight = 44
self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.allowsSelection = false self.tableView.allowsSelection = false
self.tableView.register(TunnelDetailTableViewStatusCell.self, forCellReuseIdentifier: TunnelDetailTableViewStatusCell.id) self.tableView.register(TunnelDetailTableViewStatusCell.self, forCellReuseIdentifier: TunnelDetailTableViewStatusCell.id)
self.tableView.register(TunnelDetailTableViewKeyValueCell.self, forCellReuseIdentifier: TunnelDetailTableViewKeyValueCell.id) self.tableView.register(TunnelDetailTableViewKeyValueCell.self, forCellReuseIdentifier: TunnelDetailTableViewKeyValueCell.id)
@ -315,9 +316,17 @@ class TunnelDetailTableViewKeyValueCell: CopyableLabelTableViewCell {
let keyLabel: UILabel let keyLabel: UILabel
let valueLabel: ScrollableLabel let valueLabel: ScrollableLabel
var isStackedHorizontally: Bool = false
var isStackedVertically: Bool = false
var contentSizeBasedConstraints: [NSLayoutConstraint] = []
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
keyLabel = UILabel() keyLabel = UILabel()
keyLabel.font = UIFont.preferredFont(forTextStyle: .body)
keyLabel.adjustsFontForContentSizeCategory = true
valueLabel = ScrollableLabel() valueLabel = ScrollableLabel()
valueLabel.label.font = UIFont.preferredFont(forTextStyle: .body)
valueLabel.label.adjustsFontForContentSizeCategory = true
keyLabel.textColor = UIColor.black keyLabel.textColor = UIColor.black
valueLabel.textColor = UIColor.gray valueLabel.textColor = UIColor.gray
@ -329,15 +338,14 @@ class TunnelDetailTableViewKeyValueCell: CopyableLabelTableViewCell {
keyLabel.textAlignment = .left keyLabel.textAlignment = .left
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
keyLabel.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor), keyLabel.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor),
keyLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor) keyLabel.topAnchor.constraint(equalToSystemSpacingBelow: contentView.layoutMarginsGuide.topAnchor, multiplier: 0.5)
]) ])
contentView.addSubview(valueLabel) contentView.addSubview(valueLabel)
valueLabel.translatesAutoresizingMaskIntoConstraints = false valueLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
valueLabel.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor), valueLabel.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor),
valueLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), contentView.layoutMarginsGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: valueLabel.bottomAnchor, multiplier: 0.5)
valueLabel.leftAnchor.constraint(equalTo: keyLabel.rightAnchor, constant: 8)
]) ])
// Key label should never appear truncated // Key label should never appear truncated
@ -345,6 +353,40 @@ class TunnelDetailTableViewKeyValueCell: CopyableLabelTableViewCell {
// Key label should hug it's content; value label should not. // Key label should hug it's content; value label should not.
keyLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal) keyLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
valueLabel.setContentHuggingPriority(.defaultLow, for: .horizontal) valueLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
configureForContentSize()
}
func configureForContentSize() {
var constraints: [NSLayoutConstraint] = []
if (self.traitCollection.preferredContentSizeCategory.isAccessibilityCategory) {
// Stack vertically
if (!isStackedVertically) {
constraints = [
valueLabel.topAnchor.constraint(equalToSystemSpacingBelow: keyLabel.bottomAnchor, multiplier: 0.5),
valueLabel.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor),
keyLabel.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor)
]
isStackedVertically = true
isStackedHorizontally = false
}
} else {
// Stack horizontally
if (!isStackedHorizontally) {
constraints = [
contentView.layoutMarginsGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: keyLabel.bottomAnchor, multiplier: 0.5),
valueLabel.leftAnchor.constraint(equalToSystemSpacingAfter: keyLabel.rightAnchor, multiplier: 1),
valueLabel.topAnchor.constraint(equalToSystemSpacingBelow: contentView.layoutMarginsGuide.topAnchor, multiplier: 0.5),
]
isStackedHorizontally = true
isStackedVertically = false
}
}
if (!constraints.isEmpty) {
NSLayoutConstraint.deactivate(self.contentSizeBasedConstraints)
NSLayoutConstraint.activate(constraints)
self.contentSizeBasedConstraints = constraints
}
} }
required init?(coder aDecoder: NSCoder) { required init?(coder aDecoder: NSCoder) {
@ -355,6 +397,7 @@ class TunnelDetailTableViewKeyValueCell: CopyableLabelTableViewCell {
super.prepareForReuse() super.prepareForReuse()
key = "" key = ""
value = "" value = ""
configureForContentSize()
} }
} }
@ -375,12 +418,15 @@ class TunnelDetailTableViewButtonCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
button = UIButton(type: .system) button = UIButton(type: .system)
button.titleLabel?.font = UIFont.preferredFont(forTextStyle: .body)
button.titleLabel?.adjustsFontForContentSizeCategory = true
buttonStandardTintColor = button.tintColor buttonStandardTintColor = button.tintColor
super.init(style: style, reuseIdentifier: reuseIdentifier) super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(button) contentView.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
button.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), button.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
contentView.layoutMarginsGuide.bottomAnchor.constraint(equalTo: button.bottomAnchor),
button.centerXAnchor.constraint(equalTo: contentView.centerXAnchor) button.centerXAnchor.constraint(equalTo: contentView.centerXAnchor)
]) ])
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
@ -419,6 +465,10 @@ class TunnelDetailTableViewActivateOnDemandCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .value1, reuseIdentifier: reuseIdentifier) super.init(style: .value1, reuseIdentifier: reuseIdentifier)
textLabel?.text = "Activate on demand" textLabel?.text = "Activate on demand"
textLabel?.font = UIFont.preferredFont(forTextStyle: .body)
textLabel?.adjustsFontForContentSizeCategory = true
detailTextLabel?.font = UIFont.preferredFont(forTextStyle: .body)
detailTextLabel?.adjustsFontForContentSizeCategory = true
} }
required init?(coder aDecoder: NSCoder) { required init?(coder aDecoder: NSCoder) {
@ -431,6 +481,7 @@ class TunnelDetailTableViewActivateOnDemandCell: UITableViewCell {
override func prepareForReuse() { override func prepareForReuse() {
super.prepareForReuse() super.prepareForReuse()
textLabel?.text = "Activate on demand"
detailTextLabel?.text = "" detailTextLabel?.text = ""
} }
} }