Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for insets when applying orthogonal alignment in view distribution #125

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,80 @@ final class ViewDistributionSnapshotTests: SnapshotTestCase {
)
}

@MainActor
func testHorizontalDistributionAlongBaseline() {
final class TestView: UIView {

// MARK: Initialization

override init(frame: CGRect) {
super.init(frame: frame)

referenceView.backgroundColor = .black.withAlphaComponent(0.1)
addSubview(referenceView)

label1.font = .systemFont(ofSize: 12)
label1.text = "Hello worldy"
label1.textColor = .black
addSubview(label1)

label2.font = .systemFont(ofSize: 16)
label2.text = "Hello worldy"
label2.textColor = .black
addSubview(label2)

label3.font = .boldSystemFont(ofSize: 14)
label3.text = "Hello worldy"
label3.textColor = .black
addSubview(label3)

backgroundColor = .white
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: Private

private let label1: UILabel = .init()

private let label2: UILabel = .init()

private let label3: UILabel = .init()

private let referenceView: UIView = .init()

// MARK: UIView

override func layoutSubviews() {
label1.sizeToFit()
label2.sizeToFit()
label3.sizeToFit()

applyHorizontalSubviewDistribution(
[
label1.distributionItemUsingCapInsets,
label2.distributionItemUsingCapInsets,
label3.distributionItemUsingCapInsets,
],
inRect: bounds.insetBy(bottom: 8),
orthogonalAlignment: .bottom(inset: 0)
)

referenceView.untransformedFrame = bounds.slice(from: .maxYEdge, amount: 8).slice
}

}

assertSnapshot(
matching: TestView(frame: .init(x: 0, y: 0, width: 400, height: 64)),
as: .image,
named: nameForSnapshot(with: [])
)
}

}

// MARK: -
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Paralayout/UIView+Distribution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,9 @@ extension UIView {
fatalError("Unknown user interface layout direction")
}

frame = frame.inset(by: insets)
applyOrthogonalAlignment(&frame, layoutBounds)
frame = frame.outset(by: insets)

subview.untransformedFrame = frame

Expand Down