Skip to content

Commit 902ce67

Browse files
authored
Merge pull request #126 from vandyshev/percent_margins
Percent margins
2 parents 1fda63b + 3f4fd2e commit 902ce67

File tree

3 files changed

+626
-0
lines changed

3 files changed

+626
-0
lines changed

Sources/Impl/PinLayoutImpl.swift

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,47 +877,133 @@ class PinLayoutImpl: PinLayout {
877877
return self
878878
}
879879

880+
@discardableResult
881+
func marginTop(_ percent: Percent) -> PinLayout {
882+
func context() -> String { return "marginTop(\(percent.description))" }
883+
return marginTop(percent, context)
884+
}
885+
886+
@discardableResult
887+
private func marginTop(_ percent: Percent, _ context: Context) -> Self {
888+
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
889+
marginTop = percent.of(layoutSuperviewRect.height)
890+
return self
891+
}
892+
880893
@discardableResult
881894
func marginLeft(_ value: CGFloat) -> PinLayout {
882895
marginLeft = value
883896
return self
884897
}
885898

899+
@discardableResult
900+
func marginLeft(_ percent: Percent) -> PinLayout {
901+
func context() -> String { return "marginLeft(\(percent.description))" }
902+
return marginLeft(percent, context)
903+
}
904+
905+
@discardableResult
906+
private func marginLeft(_ percent: Percent, _ context: Context) -> Self {
907+
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
908+
marginLeft = percent.of(layoutSuperviewRect.width)
909+
return self
910+
}
911+
886912
@discardableResult
887913
func marginBottom(_ value: CGFloat) -> PinLayout {
888914
marginBottom = value
889915
return self
890916
}
891917

918+
@discardableResult
919+
func marginBottom(_ percent: Percent) -> PinLayout {
920+
func context() -> String { return "marginBottom(\(percent.description))" }
921+
return marginBottom(percent, context)
922+
}
923+
924+
@discardableResult
925+
private func marginBottom(_ percent: Percent, _ context: Context) -> Self {
926+
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
927+
marginBottom = percent.of(layoutSuperviewRect.height)
928+
return self
929+
}
930+
892931
@discardableResult
893932
func marginRight(_ value: CGFloat) -> PinLayout {
894933
marginRight = value
895934
return self
896935
}
936+
937+
@discardableResult
938+
func marginRight(_ percent: Percent) -> PinLayout {
939+
func context() -> String { return "marginRight(\(percent.description))" }
940+
return marginRight(percent, context)
941+
}
942+
943+
@discardableResult
944+
private func marginRight(_ percent: Percent, _ context: Context) -> Self {
945+
guard let layoutSuperviewRect = layoutSuperviewRect(context) else { return self }
946+
marginRight = percent.of(layoutSuperviewRect.width)
947+
return self
948+
}
897949

898950
@discardableResult
899951
func marginStart(_ value: CGFloat) -> PinLayout {
900952
return isLTR() ? marginLeft(value) : marginRight(value)
901953
}
954+
955+
@discardableResult
956+
func marginStart(_ percent: Percent) -> PinLayout {
957+
func context() -> String { return "marginStart(\(percent.description))" }
958+
return isLTR() ? marginLeft(percent, context) : marginRight(percent, context)
959+
}
902960

903961
@discardableResult
904962
func marginEnd(_ value: CGFloat) -> PinLayout {
905963
return isLTR() ? marginRight(value) : marginLeft(value)
906964
}
907965

966+
@discardableResult
967+
func marginEnd(_ percent: Percent) -> PinLayout {
968+
func context() -> String { return "marginEnd(\(percent.description))" }
969+
return isLTR() ? marginRight(percent, context) : marginLeft(percent, context)
970+
}
971+
908972
@discardableResult
909973
func marginHorizontal(_ value: CGFloat) -> PinLayout {
910974
marginLeft = value
911975
marginRight = value
912976
return self
913977
}
914978

979+
@discardableResult
980+
func marginHorizontal(_ percent: Percent) -> PinLayout {
981+
func context() -> String { return "marginHorizontal(\(percent.description))" }
982+
return marginHorizontal(percent, context)
983+
}
984+
985+
@discardableResult
986+
private func marginHorizontal(_ percent: Percent, _ context: Context) -> Self {
987+
return marginLeft(percent, context).marginRight(percent, context)
988+
}
989+
915990
@discardableResult
916991
func marginVertical(_ value: CGFloat) -> PinLayout {
917992
marginTop = value
918993
marginBottom = value
919994
return self
920995
}
996+
997+
@discardableResult
998+
func marginVertical(_ percent: Percent) -> PinLayout {
999+
func context() -> String { return "marginVertical(\(percent.description))" }
1000+
return marginVertical(percent, context)
1001+
}
1002+
1003+
@discardableResult
1004+
private func marginVertical(_ percent: Percent, _ context: Context) -> Self {
1005+
return marginTop(percent, context).marginBottom(percent, context)
1006+
}
9211007

9221008
@discardableResult
9231009
func margin(_ insets: UIEdgeInsets) -> PinLayout {
@@ -947,6 +1033,15 @@ class PinLayoutImpl: PinLayout {
9471033
return self
9481034
}
9491035

1036+
@discardableResult
1037+
func margin(_ percent: Percent) -> PinLayout {
1038+
func context() -> String { return "margin(\(percent.description))" }
1039+
return marginTop(percent, context)
1040+
.marginLeft(percent, context)
1041+
.marginBottom(percent, context)
1042+
.marginRight(percent, context)
1043+
}
1044+
9501045
@discardableResult
9511046
func margin(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> PinLayout {
9521047
marginTop = top
@@ -956,6 +1051,17 @@ class PinLayoutImpl: PinLayout {
9561051
return self
9571052
}
9581053

1054+
@discardableResult
1055+
func margin(_ top: Percent, _ left: Percent, _ bottom: Percent, _ right: Percent) -> PinLayout {
1056+
func context() -> String {
1057+
return "margin(top: \(top.description), left: \(left.description), bottom: \(bottom.description), right: \(right.description)"
1058+
}
1059+
return marginTop(top, context)
1060+
.marginLeft(left, context)
1061+
.marginBottom(bottom, context)
1062+
.marginRight(right, context)
1063+
}
1064+
9591065
@discardableResult func margin(_ vertical: CGFloat, _ horizontal: CGFloat) -> PinLayout {
9601066
marginTop = vertical
9611067
marginLeft = horizontal
@@ -964,6 +1070,12 @@ class PinLayoutImpl: PinLayout {
9641070
return self
9651071
}
9661072

1073+
@discardableResult
1074+
func margin(_ vertical: Percent, _ horizontal: Percent) -> PinLayout {
1075+
func context() -> String { return "margin(vertical: \(vertical.description), horizontal: \(horizontal.description)"}
1076+
return marginVertical(vertical, context).marginHorizontal(horizontal, context)
1077+
}
1078+
9671079
@discardableResult func margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat) -> PinLayout {
9681080
marginTop = top
9691081
marginLeft = horizontal
@@ -972,6 +1084,12 @@ class PinLayoutImpl: PinLayout {
9721084
return self
9731085
}
9741086

1087+
@discardableResult
1088+
func margin(_ top: Percent, _ horizontal: Percent, _ bottom: Percent) -> PinLayout {
1089+
func context() -> String { return "margin(top: \(top.description), horizontal: \(horizontal.description), bottom: \(bottom.description)"}
1090+
return marginTop(top, context).marginHorizontal(horizontal, context).marginBottom(bottom, context)
1091+
}
1092+
9751093
@discardableResult
9761094
func pinEdges() -> PinLayout {
9771095
shouldPinEdges = true

Sources/PinLayout.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,21 +423,29 @@ public protocol PinLayout {
423423
Set the top margin.
424424
*/
425425
@discardableResult func marginTop(_ value: CGFloat) -> PinLayout
426+
427+
@discardableResult func marginTop(_ percent: Percent) -> PinLayout
426428

427429
/**
428430
Set the left margin.
429431
*/
430432
@discardableResult func marginLeft(_ value: CGFloat) -> PinLayout
433+
434+
@discardableResult func marginLeft(_ percent: Percent) -> PinLayout
431435

432436
/**
433437
Set the bottom margin.
434438
*/
435439
@discardableResult func marginBottom(_ value: CGFloat) -> PinLayout
440+
441+
@discardableResult func marginBottom(_ percent: Percent) -> PinLayout
436442

437443
/**
438444
Set the right margin.
439445
*/
440446
@discardableResult func marginRight(_ value: CGFloat) -> PinLayout
447+
448+
@discardableResult func marginRight(_ percent: Percent) -> PinLayout
441449

442450
// RTL support
443451
/**
@@ -448,6 +456,8 @@ public protocol PinLayout {
448456
* In RTL direction, start margin specify the **right** margin.
449457
*/
450458
@discardableResult func marginStart(_ value: CGFloat) -> PinLayout
459+
460+
@discardableResult func marginStart(_ percent: Percent) -> PinLayout
451461

452462
/**
453463
Set the end margin.
@@ -457,17 +467,23 @@ public protocol PinLayout {
457467
* In RTL direction, end margin specify the **left** margin.
458468
*/
459469
@discardableResult func marginEnd(_ value: CGFloat) -> PinLayout
470+
471+
@discardableResult func marginEnd(_ percent: Percent) -> PinLayout
460472

461473
/**
462474
Set the left, right, start and end margins to the specified value.
463475
*/
464476
@discardableResult func marginHorizontal(_ value: CGFloat) -> PinLayout
477+
478+
@discardableResult func marginHorizontal(_ percent: Percent) -> PinLayout
465479

466480
/**
467481
Set the top and bottom margins to the specified value.
468482
*/
469483
@discardableResult func marginVertical(_ value: CGFloat) -> PinLayout
470484

485+
@discardableResult func marginVertical(_ percent: Percent) -> PinLayout
486+
471487
/**
472488
Set all margins using UIEdgeInsets.
473489
This method is particularly useful to set all margins using iOS 11 `UIView.safeAreaInsets`.
@@ -487,22 +503,30 @@ public protocol PinLayout {
487503
Set all margins to the specified value.
488504
*/
489505
@discardableResult func margin(_ value: CGFloat) -> PinLayout
506+
507+
@discardableResult func margin(_ percent: Percent) -> PinLayout
490508

491509
/**
492510
Set individually vertical margins (top, bottom) and horizontal margins (left, right, start, end).
493511
*/
494512
@discardableResult func margin(_ vertical: CGFloat, _ horizontal: CGFloat) -> PinLayout
513+
514+
@discardableResult func margin(_ vertical: Percent, _ horizontal: Percent) -> PinLayout
495515

496516
/**
497517
Set individually top, horizontal margins and bottom margin.
498518
*/
499519
@discardableResult func margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat) -> PinLayout
520+
521+
@discardableResult func margin(_ top: Percent, _ horizontal: Percent, _ bottom: Percent) -> PinLayout
500522

501523
/**
502524
Set individually top, left, bottom and right margins.
503525
*/
504526
@discardableResult func margin(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> PinLayout
505527

528+
@discardableResult func margin(_ top: Percent, _ left: Percent, _ bottom: Percent, _ right: Percent) -> PinLayout
529+
506530
/// Normally if only either left or right has been specified, PinLayout will MOVE the view to apply left or right margins.
507531
/// This is also true even if the width has been set.
508532
/// Calling pinEdges() will force PinLayout to pin the four edges and then apply left and/or right margins, and this without

0 commit comments

Comments
 (0)