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

Add feature for Gap #241

Merged
merged 12 commits into from
Jan 11, 2024
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,23 @@ This property takes the same values as the width and height properties, and spec

<br>

### gap
- Applies to: `flex containers`
- CSS name: `gap`

**Method:**

* **`columnGap(_ value: CGFloat)`**
This property set distance between columns.

* **`rownGap(_ value: CGFloat)`**
Skyline-23 marked this conversation as resolved.
Show resolved Hide resolved
This property set distance between rows.

* **`gap(_ value: CGFloat)`**
This property set distance between both of rows and columns.

<br/>

### isIncludedInLayout()
- Applies to: `flex items`

Expand Down
43 changes: 43 additions & 0 deletions Sources/Swift/FlexLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,49 @@ public final class Flex {
return self
}

//
// MARK: Gap
//

/**
Set distance between columns.

- Parameters:
- value: distance
- Returns: flex interface
*/
@discardableResult
public func columnGap(_ value: CGFloat) -> Flex {
yoga.columnGap = value
return self
}

/**
Set distance between rows.

- Parameters:
- value: distance
- Returns: flex interface
*/
@discardableResult
public func rowGap(_ value: CGFloat) -> Flex {
yoga.rowGap = value
return self
}

/**
Set distance between both of rows and columns.

- Parameters:
- value: distance
- Returns: flex interface
*/
@discardableResult
public func gap(_ value: CGFloat) -> Flex {
yoga.gap = value
return self
}

//
// MARK: UIView Visual properties
//
Expand Down
4 changes: 4 additions & 0 deletions Sources/YogaKit/include/YogaKit/YGLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ typedef NS_OPTIONS(NSInteger, YGDimensionFlexibility) {
// Yoga specific properties, not compatible with flexbox specification
@property(nonatomic, readwrite, assign) CGFloat aspectRatio;

@property(nonatomic, readwrite, assign) CGFloat columnGap;
@property(nonatomic, readwrite, assign) CGFloat rowGap;
@property(nonatomic, readwrite, assign) CGFloat gap;

/**
Get the resolved direction of this node. This won't be YGDirectionInherit
*/
Expand Down
49 changes: 49 additions & 0 deletions Tests/FlexLayoutTests/WidthSizeContentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ final class WidthSizeContentTests: XCTestCase {
var viewController: UIViewController!
var rootFlexContainer: UIView!
var aView: UIView!
var bView: UIView!
var cView: UIView!

override func setUp() {
super.setUp()
Expand All @@ -29,6 +31,8 @@ final class WidthSizeContentTests: XCTestCase {
viewController.view.addSubview(rootFlexContainer)

aView = UIView()
bView = UIView()
cView = UIView()
}

func test_basis_adjust_aView_size_and_position() {
Expand Down Expand Up @@ -159,4 +163,49 @@ final class WidthSizeContentTests: XCTestCase {
rootFlexContainer.flex.layout()
XCTAssertEqual(aView.frame, CGRect(x: 0.0, y: 0.0, width: 400.0, height: 200.0))
}

func test_row_gap() {
rootFlexContainer.flex.direction(.row).define { flex in
flex.addItem(aView).height(100).width(100)
flex.addItem(bView).height(100).width(100)
flex.alignItems(.start)

flex.columnGap(50)
}

rootFlexContainer.flex.layout()
XCTAssertEqual(bView.frame, CGRect(x: 150.0, y: 0.0, width: 100.0, height: 100.0))
}

func test_column_gap() {
rootFlexContainer.flex.direction(.column).define { flex in
flex.addItem(aView).height(100).width(100)
flex.addItem(bView).height(100).width(100)
flex.alignItems(.start)

flex.rowGap(50)
}

rootFlexContainer.flex.layout()
XCTAssertEqual(bView.frame, CGRect(x: 0.0, y: 150.0, width: 100.0, height: 100.0))
}

func test_gap() {
rootFlexContainer.flex.direction(.column).define { flex in
flex.addItem().direction(.row).define { flex in
flex.addItem(aView).height(100).width(100)
flex.addItem(bView).height(100).width(100)
flex.gap(50)
}
flex.addItem(cView).height(100).width(100)

flex.alignItems(.start)
flex.gap(50)
}

rootFlexContainer.flex.layout()

XCTAssertEqual(bView.frame, CGRect(x: 150.0, y: 0.0, width: 100.0, height: 100.0))
XCTAssertEqual(cView.frame, CGRect(x: 0.0, y: 150.0, width: 100.0, height: 100.0))
}
}