Skip to content

Commit 0e75735

Browse files
author
Luc Dion
committed
Add width/height methods taking percentage parameter
1 parent 68a755e commit 0e75735

File tree

6 files changed

+163
-14
lines changed

6 files changed

+163
-14
lines changed

FlexLayout.xcodeproj/project.pbxproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
2458137F1F4A21CF00CD676B /* PaddingSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2458137E1F4A21CF00CD676B /* PaddingSpec.swift */; };
1212
249008D41F165D4C00BBE9CD /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 24DA376A1EF843C500D1AB2F /* Info.plist */; };
1313
24A91B281F45E24B00AC8B3E /* MarginSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24A91B271F45E24B00AC8B3E /* MarginSpec.swift */; };
14+
24CC0E401F96807400BEE2CF /* Percent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24CC0E3F1F96807400BEE2CF /* Percent.swift */; };
1415
24DA37641EF843C500D1AB2F /* FlexLayout.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 24DA375A1EF843C500D1AB2F /* FlexLayout.framework */; };
1516
24DA37691EF843C500D1AB2F /* FlexLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24DA37681EF843C500D1AB2F /* FlexLayoutTests.swift */; };
1617
24DA37881EF8610400D1AB2F /* FlexLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24DA37871EF8610400D1AB2F /* FlexLayout.swift */; };
@@ -35,6 +36,7 @@
3536
244F4ABD1F8196E900460B54 /* YogaKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = YogaKit.framework; path = Carthage/Build/iOS/YogaKit.framework; sourceTree = "<group>"; };
3637
2458137E1F4A21CF00CD676B /* PaddingSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaddingSpec.swift; sourceTree = "<group>"; };
3738
24A91B271F45E24B00AC8B3E /* MarginSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MarginSpec.swift; sourceTree = "<group>"; };
39+
24CC0E3F1F96807400BEE2CF /* Percent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Percent.swift; path = Sources/Percent.swift; sourceTree = SOURCE_ROOT; };
3840
24DA375A1EF843C500D1AB2F /* FlexLayout.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FlexLayout.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3941
24DA37631EF843C500D1AB2F /* FlexLayoutTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FlexLayoutTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4042
24DA37681EF843C500D1AB2F /* FlexLayoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlexLayoutTests.swift; sourceTree = "<group>"; };
@@ -99,6 +101,7 @@
99101
isa = PBXGroup;
100102
children = (
101103
24DA37871EF8610400D1AB2F /* FlexLayout.swift */,
104+
24CC0E3F1F96807400BEE2CF /* Percent.swift */,
102105
DF9174751F1D6DA20034632D /* Impl */,
103106
2429E4BD1F19181F00BBC032 /* Supporting Files */,
104107
);
@@ -316,6 +319,7 @@
316319
buildActionMask = 2147483647;
317320
files = (
318321
DF9174791F1D6DEA0034632D /* UIView+FlexLayout.swift in Sources */,
322+
24CC0E401F96807400BEE2CF /* Percent.swift in Sources */,
319323
DF9174771F1D6DC00034632D /* FlexLayout+Enum.swift in Sources */,
320324
24DA37881EF8610400D1AB2F /* FlexLayout.swift in Sources */,
321325
);
@@ -386,7 +390,7 @@
386390
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
387391
GCC_WARN_UNUSED_FUNCTION = YES;
388392
GCC_WARN_UNUSED_VARIABLE = YES;
389-
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
393+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
390394
MTL_ENABLE_DEBUG_INFO = YES;
391395
ONLY_ACTIVE_ARCH = YES;
392396
SDKROOT = iphoneos;
@@ -435,7 +439,7 @@
435439
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
436440
GCC_WARN_UNUSED_FUNCTION = YES;
437441
GCC_WARN_UNUSED_VARIABLE = YES;
438-
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
442+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
439443
MTL_ENABLE_DEBUG_INFO = NO;
440444
SDKROOT = iphoneos;
441445
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";

FlexLayout/Percent.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2017 Luc Dion
2+
// Permission is hereby granted, free of charge, to any person obtaining a copy
3+
// of this software and associated documentation files (the "Software"), to deal
4+
// in the Software without restriction, including without limitation the rights
5+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6+
// copies of the Software, and to permit persons to whom the Software is
7+
// furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in
10+
// all copies or substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18+
// THE SOFTWARE.
19+
20+
import UIKit
21+
22+
public struct Percent {
23+
let value: CGFloat
24+
}
25+
26+
postfix operator %
27+
public postfix func % (v: CGFloat) -> Percent {
28+
return Percent(value: v)
29+
}
30+
31+
public postfix func % (v: Int) -> Percent {
32+
return Percent(value: CGFloat(v))
33+
}
34+
35+
prefix operator -
36+
public prefix func - (p: Percent) -> Percent {
37+
return Percent(value: -p.value)
38+
}

Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- FlexLayout (1.1.1):
2+
- FlexLayout (1.1.3):
33
- YogaKit (~> 1.6)
44
- Nimble (7.0.1)
55
- PinLayout (1.2.3)
@@ -24,7 +24,7 @@ EXTERNAL SOURCES:
2424
:path: ./
2525

2626
SPEC CHECKSUMS:
27-
FlexLayout: 64e758c6951a826cb7d54f09874977ad84731cb1
27+
FlexLayout: 8a65c1c9426b9567b81bd0205c2b0adb670bfef4
2828
Nimble: 657d000e11df8aebe27cdaf9d244de7f30ed87f7
2929
PinLayout: 96d403c7da4e5168ba238f0401b7fed24ebfdd6f
3030
Quick: dafc587e21eed9f4cab3249b9f9015b0b7a7f71d

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Flexbox is an incredible improvement over UIStackView. It is simpler to use, muc
2323
2424

2525
### Requirements
26-
* iOS 8.0+
26+
* iOS 9.0+
2727
* Xcode 8.0+ / Xcode 9.0
2828
* Swift 3.0+ / Swift 4.0
2929

@@ -637,10 +637,12 @@ For example, if all items have `grow` set to 1, every child will set to an equal
637637

638638
* **`shrink(_: CGFloat)`**
639639
It specifies the "flex shrink factor", which determines how much the flex item will shrink relative to the rest of the flex items in the flex container when there isn't enough space on the main-axis.
640-
640+
641641
When omitted, it is set to 0 and the flex shrink factor is multiplied by the flex `basis` when distributing negative space.
642642

643643
A shrink value of 0 keeps the view's size in the main-axis direction. Note that this may cause the view to overflow its flex container.
644+
645+
Shrink is about proportions. If an item has a shrink of 3, and the rest have a shrink of 1, the item will shrink 3x as fast as the rest.
644646

645647
<br>
646648

@@ -781,8 +783,12 @@ FlexLayout has methods to set the view’s height and width.
781783

782784
* **`width(_ width: CGFloat)`**
783785
The value specifies the view's width in pixels. The value must be non-negative.
786+
* **`width(_ percent: FPercent)`**
787+
The value specifies the view's width in percentage of its container width. The value must be non-negative.
784788
* **`height(_ height: CGFloat)`**
785789
The value specifies the view's height in pixels. The value must be non-negative.
790+
* **`height(_ percent: FPercent)`**
791+
The value specifies the view's height in percentage of its container height. The value must be non-negative.
786792
* **`size(_ size: CGSize)`**
787793
The value specifies view's width and the height in pixels. Values must be non-negative.
788794
* **`size(_ sideLength: CGFloat)`**
@@ -792,6 +798,7 @@ The value specifies the width and the height of the view in pixels, creating a s
792798
###### Usage examples:
793799
```swift
794800
view.flex.width(100)
801+
view.flex.width(50%)
795802
view.flex.height(200)
796803

797804
view.flex.size(250)
@@ -815,23 +822,29 @@ Another case where Min and Max dimension constraints are useful is when using `a
815822

816823
* **`minWidth(_ width: CGFloat)`**
817824
The value specifies the view's minimum width of the view in pixels. The value must be non-negative.
818-
825+
* **`minWidth(_ percent: FPercent)`**
826+
The value specifies the view's minimum width of the view in percentage of its container width. The value must be non-negative.
819827
* **`maxWidth(_ width: CGFloat)`**
820828
The value specifies the view's maximum width of the view in pixels. The value must be non-negative.
821-
829+
* **`maxWidth(_ percent: FPercent)`**
830+
The value specifies the view's maximum width of the view in percentage of its container width. The value must be non-negative.
822831
* **`minHeight(_ height: CGFloat)`**
823832
The value specifies the view's minimum height of the view in pixels. The value must be non-negative.
824-
833+
* **`minHeight(_ percent: FPercent)`**
834+
The value specifies the view's minimum height of the view in percentage of its container height. The value must be non-negative.
825835
* **`maxHeight(_ height: CGFloat)`**
826836
The value specifies the view's maximum height of the view in pixels. The value must be non-negative.
837+
* **`maxHeight(_ percent: FPercent)`**
838+
The value specifies the view's maximum height of the view in percentage of its container height. The value must be non-negative.
827839

828840
###### Usage examples:
829841
```swift
830842
view.flex.maxWidth(200)
843+
view.flex.maxWidth(50%)
831844
view.flex.width(of: view1).maxWidth(250)
832845

833846
view.flex.maxHeight(100)
834-
view.flex.height(of: view1).maxHeight(200)
847+
view.flex.height(of: view1).maxHeight(30%)
835848
```
836849
<br>
837850

Sources/FlexLayout.swift

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,16 @@ public class Flex {
341341
return self
342342
}
343343

344+
/**
345+
The value specifies the view's width in percentage of its container width. The value must be non-negative.
346+
Example: view.flex.width(20%)
347+
*/
348+
@discardableResult
349+
public func width(_ percent: FPercent) -> Flex {
350+
view.yoga.width = YGValue(value: Float(percent.value), unit: .percent)
351+
return self
352+
}
353+
344354
/**
345355
The value specifies the view's height in pixels. The value must be non-negative.
346356
*/
@@ -350,6 +360,16 @@ public class Flex {
350360
return self
351361
}
352362

363+
/**
364+
The value specifies the view's height in percentage of its container height. The value must be non-negative.
365+
Example: view.flex.height(40%)
366+
*/
367+
@discardableResult
368+
public func height(_ percent: FPercent) -> Flex {
369+
view.yoga.height = YGValue(value: Float(percent.value), unit: .percent)
370+
return self
371+
}
372+
353373
/**
354374
The value specifies view's width and the height in pixels. Values must be non-negative.
355375
*/
@@ -371,16 +391,25 @@ public class Flex {
371391
}
372392

373393
/**
374-
The value specifies the view's minimum width of the view in pixels. The value must be non-negative.
394+
The value specifies the view's minimum width in pixels. The value must be non-negative.
375395
*/
376396
@discardableResult
377397
public func minWidth(_ value: CGFloat) -> Flex {
378398
view.yoga.minWidth = YGValue(value)
379399
return self
380400
}
401+
402+
/**
403+
The value specifies the view's minimum width in percentage of its container width. The value must be non-negative.
404+
*/
405+
@discardableResult
406+
public func minWidth(_ percent: FPercent) -> Flex {
407+
view.yoga.minWidth = YGValue(value: Float(percent.value), unit: .percent)
408+
return self
409+
}
381410

382411
/**
383-
The value specifies the view's maximum width of the view in pixels. The value must be non-negative.
412+
The value specifies the view's maximum width in pixels. The value must be non-negative.
384413
*/
385414
@discardableResult
386415
public func maxWidth(_ value: CGFloat) -> Flex {
@@ -389,23 +418,50 @@ public class Flex {
389418
}
390419

391420
/**
392-
The value specifies the view's minimum height of the view in pixels. The value must be non-negative.
421+
The value specifies the view's maximum width in percentage of its container width. The value must be non-negative.
422+
*/
423+
@discardableResult
424+
public func maxWidth(_ percent: FPercent) -> Flex {
425+
view.yoga.maxWidth = YGValue(value: Float(percent.value), unit: .percent)
426+
return self
427+
}
428+
429+
/**
430+
The value specifies the view's minimum height in pixels. The value must be non-negative.
393431
*/
394432
@discardableResult
395433
public func minHeight(_ value: CGFloat) -> Flex {
396434
view.yoga.minHeight = YGValue(value)
397435
return self
398436
}
437+
438+
/**
439+
The value specifies the view's minimum height in percentage of its container height. The value must be non-negative.
440+
*/
441+
@discardableResult
442+
public func minHeight(_ percent: FPercent) -> Flex {
443+
view.yoga.minHeight = YGValue(value: Float(percent.value), unit: .percent)
444+
return self
445+
}
399446

400447
/**
401-
The value specifies the view's maximum height of the view in pixels. The value must be non-negative.
448+
The value specifies the view's maximum height in pixels. The value must be non-negative.
402449
*/
403450
@discardableResult
404451
public func maxHeight(_ value: CGFloat) -> Flex {
405452
view.yoga.maxHeight = YGValue(value)
406453
return self
407454
}
408455

456+
/**
457+
The value specifies the view's maximum height in percentage of its container height. The value must be non-negative.
458+
*/
459+
@discardableResult
460+
public func maxHeight(_ percent: FPercent) -> Flex {
461+
view.yoga.maxHeight = YGValue(value: Float(percent.value), unit: .percent)
462+
return self
463+
}
464+
409465
/**
410466
AspectRatio is a property introduced by Yoga that don't exist in CSS. AspectRatio solves the problem of knowing
411467
one dimension of an element and an aspect ratio, this is very common when it comes to images, videos, and other

Sources/Percent.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2017 Luc Dion
2+
// Permission is hereby granted, free of charge, to any person obtaining a copy
3+
// of this software and associated documentation files (the "Software"), to deal
4+
// in the Software without restriction, including without limitation the rights
5+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6+
// copies of the Software, and to permit persons to whom the Software is
7+
// furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in
10+
// all copies or substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18+
// THE SOFTWARE.
19+
20+
import UIKit
21+
22+
public struct FPercent {
23+
let value: CGFloat
24+
}
25+
26+
postfix operator %
27+
public postfix func % (v: CGFloat) -> FPercent {
28+
return FPercent(value: v)
29+
}
30+
31+
public postfix func % (v: Int) -> FPercent {
32+
return FPercent(value: CGFloat(v))
33+
}
34+
35+
prefix operator -
36+
public prefix func - (p: FPercent) -> FPercent {
37+
return FPercent(value: -p.value)
38+
}

0 commit comments

Comments
 (0)