Skip to content

Commit

Permalink
Merge pull request #42 from square/kve/add-fill-to-aligned
Browse files Browse the repository at this point in the history
Aligned Additions
  • Loading branch information
kyleve authored Dec 17, 2019
2 parents 98b99fd + d037231 commit dbfda59
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ matrix:
xcode_destination: platform=iOS Simulator,OS=9.3,name=iPhone 4s
before_install:
- cd SampleApp
- gem update --system
- gem install bundler
- bundle install
- bundle exec pod repo update
- bundle exec pod install
Expand Down
16 changes: 14 additions & 2 deletions BlueprintUI/Sources/Layout/Aligned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import UIKit

/// Aligns a content element within itself. The vertical and horizontal alignment may be set independently.
///
/// The size of the content element is determined by calling `measure(in:)` on
/// the content element – even if that size is larger than the wrapping element.
/// When using alignment mode `.fill`, the content is scaled to the width or height of the `Aligned` element.
///
/// For other modes, the size of the content element is determined by calling `measure(in:)`
/// on the content element – even if that size is larger than the wrapping element.
///
public struct Aligned: Element {
/// The possible vertical alignment values.
Expand All @@ -14,6 +16,8 @@ public struct Aligned: Element {
case center
/// Aligns the content to the bottom edge of the containing element.
case bottom
/// The content fills the full vertical height of the containing element.
case fill
}

/// The possible horizontal alignment values.
Expand All @@ -26,6 +30,8 @@ public struct Aligned: Element {
/// Aligns the content to the trailing edge of the containing element.
/// In left-to-right languages, this is the right edge.
case trailing
/// The content fills the full horizontal width of the containing element.
case fill
}

/// The content element to be aligned.
Expand Down Expand Up @@ -80,6 +86,9 @@ public struct Aligned: Element {
attributes.frame.origin.y = (size.height - contentSize.height) / 2.0
case .bottom:
attributes.frame.origin.y = size.height - contentSize.height
case .fill:
attributes.frame.origin.y = 0
attributes.frame.size.height = size.height
}

switch horizontalAlignment {
Expand All @@ -89,6 +98,9 @@ public struct Aligned: Element {
attributes.frame.origin.x = (size.width - contentSize.width) / 2.0
case .trailing:
attributes.frame.origin.x = size.width - contentSize.width
case .fill:
attributes.frame.origin.x = 0
attributes.frame.size.width = size.width
}

// TODO: screen-scale round here once that lands
Expand Down
20 changes: 20 additions & 0 deletions BlueprintUI/Tests/AlignedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class AlignedTests: XCTestCase {
XCTAssertEqual(frame.maxX, 5000)
XCTAssertTrue(children[0].element is TestElement)
}

func test_horizontalFill() {
let children = childLayoutResultNodesAligned(horizontally: .fill)

XCTAssertEqual(children.count, 1)
let frame = children[0].layoutAttributes.frame
XCTAssertEqual(frame.minX, 0.0)
XCTAssertEqual(frame.maxX, 5000)
XCTAssertTrue(children[0].element is TestElement)
}

func test_verticalTop() {
let children = childLayoutResultNodesAligned(vertically: .top)
Expand Down Expand Up @@ -86,6 +96,16 @@ class AlignedTests: XCTestCase {
XCTAssertEqual(frame.maxY, 6000)
XCTAssertTrue(children[0].element is TestElement)
}

func test_verticalFill() {
let children = childLayoutResultNodesAligned(vertically: .fill)

XCTAssertEqual(children.count, 1)
let frame = children[0].layoutAttributes.frame
XCTAssertEqual(frame.minY, 0)
XCTAssertEqual(frame.maxY, 6000)
XCTAssertTrue(children[0].element is TestElement)
}
}

private struct TestElement: Element {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -38,8 +36,18 @@
ReferencedContainer = "container:SampleApp.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3F0DBD15A7E34A7177B40DF27414BFF9"
BuildableName = "BlueprintUI-Unit-Tests.xctest"
BlueprintName = "BlueprintUI-Unit-Tests"
ReferencedContainer = "container:Pods/Pods.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -61,8 +69,6 @@
ReferencedContainer = "container:SampleApp.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down

0 comments on commit dbfda59

Please sign in to comment.