Fix dynamic type edit cell layout bug
This commit is contained in:
parent
2662352541
commit
95430913bd
|
@ -19,7 +19,6 @@ class MasterFeedTableViewCell : UITableViewCell {
|
|||
|
||||
weak var delegate: MasterFeedTableViewCellDelegate?
|
||||
var allowDisclosureSelection = false
|
||||
private var layout: MasterFeedTableViewCellLayout?
|
||||
|
||||
override var accessibilityLabel: String? {
|
||||
set {}
|
||||
|
@ -53,7 +52,6 @@ class MasterFeedTableViewCell : UITableViewCell {
|
|||
var shouldShowImage = false {
|
||||
didSet {
|
||||
if shouldShowImage != oldValue {
|
||||
layout = nil
|
||||
setNeedsLayout()
|
||||
}
|
||||
faviconImageView.image = shouldShowImage ? faviconImage : nil
|
||||
|
@ -68,7 +66,6 @@ class MasterFeedTableViewCell : UITableViewCell {
|
|||
if unreadCountView.unreadCount != newValue {
|
||||
unreadCountView.unreadCount = newValue
|
||||
unreadCountView.isHidden = (newValue < 1)
|
||||
layout = nil
|
||||
setNeedsLayout()
|
||||
}
|
||||
}
|
||||
|
@ -81,7 +78,6 @@ class MasterFeedTableViewCell : UITableViewCell {
|
|||
set {
|
||||
if titleView.text != newValue {
|
||||
titleView.text = newValue
|
||||
layout = nil
|
||||
setNeedsLayout()
|
||||
}
|
||||
}
|
||||
|
@ -109,30 +105,22 @@ class MasterFeedTableViewCell : UITableViewCell {
|
|||
commonInit()
|
||||
}
|
||||
|
||||
override func prepareForReuse() {
|
||||
layout = nil
|
||||
unreadCountView.setNeedsLayout()
|
||||
unreadCountView.setNeedsDisplay()
|
||||
}
|
||||
|
||||
override func willTransition(to state: UITableViewCell.StateMask) {
|
||||
super.willTransition(to: state)
|
||||
showingEditControl = state.contains(.showingEditControl)
|
||||
}
|
||||
|
||||
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
||||
if layout == nil {
|
||||
resetLayout()
|
||||
}
|
||||
return CGSize(width: bounds.width, height: layout!.height)
|
||||
let shouldShowDisclosure = !(showingEditControl && showsReorderControl)
|
||||
let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: shouldShowDisclosure)
|
||||
return CGSize(width: bounds.width, height: layout.height)
|
||||
}
|
||||
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
if layout == nil {
|
||||
resetLayout()
|
||||
}
|
||||
layoutWith(layout!)
|
||||
let shouldShowDisclosure = !(showingEditControl && showsReorderControl)
|
||||
let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: shouldShowDisclosure)
|
||||
layoutWith(layout)
|
||||
}
|
||||
|
||||
@objc func buttonPressed(_ sender: UIButton) {
|
||||
|
@ -184,11 +172,6 @@ private extension MasterFeedTableViewCell {
|
|||
view.translatesAutoresizingMaskIntoConstraints = false
|
||||
}
|
||||
|
||||
func resetLayout() {
|
||||
let shouldShowDisclosure = !(showingEditControl && showsReorderControl)
|
||||
layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, shouldShowImage: shouldShowImage, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: shouldShowDisclosure)
|
||||
}
|
||||
|
||||
func layoutWith(_ layout: MasterFeedTableViewCellLayout) {
|
||||
faviconImageView.setFrameIfNotEqual(layout.faviconRect)
|
||||
titleView.setFrameIfNotEqual(layout.titleRect)
|
||||
|
|
|
@ -19,6 +19,8 @@ struct MasterFeedTableViewCellLayout {
|
|||
private static let unreadCountMarginLeft = CGFloat(integerLiteral: 8)
|
||||
private static let unreadCountMarginRight = CGFloat(integerLiteral: 0)
|
||||
private static let disclosureButtonSize = CGSize(width: 44, height: 44)
|
||||
|
||||
private static let minRowHeight = CGFloat(integerLiteral: 44)
|
||||
|
||||
let faviconRect: CGRect
|
||||
let titleRect: CGRect
|
||||
|
@ -64,9 +66,13 @@ struct MasterFeedTableViewCellLayout {
|
|||
}
|
||||
|
||||
// Title
|
||||
let labelWidth = bounds.width - (rFavicon.width + MasterFeedTableViewCellLayout.imageMarginRight + MasterFeedTableViewCellLayout.unreadCountMarginLeft + rUnread.width + MasterFeedTableViewCellLayout.unreadCountMarginRight + rDisclosure.width)
|
||||
let labelWidth = bounds.width - (rFavicon.width + MasterFeedTableViewCellLayout.imageMarginRight + MasterFeedTableViewCellLayout.unreadCountMarginLeft + rUnread.width + MasterFeedTableViewCellLayout.unreadCountMarginRight + MasterFeedTableViewCellLayout.disclosureButtonSize.width)
|
||||
let labelSizeInfo = MultilineUILabelSizer.size(for: label.text ?? "", font: label.font, numberOfLines: 0, width: Int(floor(labelWidth)))
|
||||
|
||||
if label.text == "inessential" {
|
||||
print("Number of lines: \(labelSizeInfo.numberOfLinesUsed) height: \(labelSizeInfo.size.height)")
|
||||
}
|
||||
|
||||
var rLabel = CGRect(x: 0.0, y: 0.0, width: labelSizeInfo.size.width, height: labelSizeInfo.size.height)
|
||||
if shouldShowImage {
|
||||
rLabel.origin.x = rFavicon.maxX + MasterFeedTableViewCellLayout.imageMarginRight
|
||||
|
@ -75,8 +81,11 @@ struct MasterFeedTableViewCellLayout {
|
|||
}
|
||||
|
||||
// Determine cell height
|
||||
let cellHeight = [rFavicon, rLabel, rUnread, rDisclosure].maxY()
|
||||
|
||||
var cellHeight = [rFavicon, rLabel, rUnread, rDisclosure].maxY()
|
||||
if cellHeight < MasterFeedTableViewCellLayout.minRowHeight {
|
||||
cellHeight = MasterFeedTableViewCellLayout.minRowHeight
|
||||
}
|
||||
|
||||
// Center in Cell
|
||||
let newBounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.width, height: cellHeight)
|
||||
|
||||
|
|
Loading…
Reference in New Issue