Skip to content

Commit 88b34e0

Browse files
author
Luc Dion
authored
Merge pull request #82 from mirego/add_marge_edgeinsets
Add margin method margin(_ insets: UIEdgeInsets)
2 parents 5ff9b00 + 2efd88e commit 88b34e0

File tree

6 files changed

+149
-47
lines changed

6 files changed

+149
-47
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
source 'https://rubygems.org'
22
gem 'synx'
3-
gem 'cocoapods', '~> 1.2.1'
3+
gem 'cocoapods'
44
gem 'jazzy'

Gemfile.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ GEM
99
tzinfo (~> 1.1)
1010
claide (1.0.2)
1111
clamp (0.6.5)
12-
cocoapods (1.2.1)
12+
cocoapods (1.3.1)
1313
activesupport (>= 4.0.2, < 5)
14-
claide (>= 1.0.1, < 2.0)
15-
cocoapods-core (= 1.2.1)
14+
claide (>= 1.0.2, < 2.0)
15+
cocoapods-core (= 1.3.1)
1616
cocoapods-deintegrate (>= 1.0.1, < 2.0)
1717
cocoapods-downloader (>= 1.1.3, < 2.0)
1818
cocoapods-plugins (>= 1.0.0, < 2.0)
@@ -27,9 +27,9 @@ GEM
2727
molinillo (~> 0.5.7)
2828
nap (~> 1.0)
2929
ruby-macho (~> 1.1)
30-
xcodeproj (>= 1.4.4, < 2.0)
31-
cocoapods-core (1.2.1)
32-
activesupport (>= 4.0.2, < 5)
30+
xcodeproj (>= 1.5.1, < 2.0)
31+
cocoapods-core (1.3.1)
32+
activesupport (>= 4.0.2, < 6)
3333
fuzzy_match (~> 2.0.4)
3434
nap (~> 1.0)
3535
cocoapods-deintegrate (1.0.1)
@@ -60,7 +60,7 @@ GEM
6060
sqlite3 (~> 1.3)
6161
xcinvoke (~> 0.3.0)
6262
liferaft (0.0.6)
63-
minitest (5.10.2)
63+
minitest (5.10.3)
6464
molinillo (0.5.7)
6565
mustache (0.99.8)
6666
nanaimo (0.2.3)
@@ -88,7 +88,7 @@ GEM
8888
thread_safe (~> 0.1)
8989
xcinvoke (0.3.0)
9090
liferaft (~> 0.0.6)
91-
xcodeproj (1.5.0)
91+
xcodeproj (1.5.1)
9292
CFPropertyList (~> 2.3.3)
9393
claide (>= 1.0.2, < 2.0)
9494
colored2 (~> 3.1)
@@ -98,7 +98,7 @@ PLATFORMS
9898
ruby
9999

100100
DEPENDENCIES
101-
cocoapods (~> 1.2.1)
101+
cocoapods
102102
jazzy
103103
synx
104104

README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -904,18 +904,27 @@ PinLayout has methods to apply margins.
904904

905905
**Methods:**
906906

907-
* `marginTop(_ value: CGFloat)`
908-
* `marginLeft(_ value: CGFloat)`
909-
* `marginBottom(_ value: CGFloat)`
910-
* `marginRight(_ value: CGFloat)`
911-
* `marginStart(_ value: CGFloat)`:left_right_arrow:
912-
* `marginEnd(_ value: CGFloat)`:left_right_arrow:
913-
* `marginHorizontal(_ value: CGFloat)`
914-
* `marginVertical(_ value: CGFloat)`
915-
* `margin(_ value: CGFloat) `
916-
* `margin(_ vertical: CGFloat, _ horizontal: CGFloat)`
917-
* `margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat)`
918-
* `margin(_ top: CGFloat, _ right: CGFloat, _ bottom: CGFloat, _ left: CGFloat) `
907+
* **`marginTop(_ value: CGFloat)`**
908+
* **`marginLeft(_ value: CGFloat)`**
909+
* **`marginBottom(_ value: CGFloat)`**
910+
* **`marginRight(_ value: CGFloat)`**
911+
* **`marginStart(_ value: CGFloat)`**:left_right_arrow:
912+
Set the start margin. Depends on the value of `Pin.layoutDirection(...)`. In LTR direction, start margin specify the **left** margin. In RTL direction, start margin specify the **right** margin.
913+
* **`marginEnd(_ value: CGFloat)`**:left_right_arrow:
914+
Set the end margin. Depends on the value of `Pin.layoutDirection(...)`. In LTR direction, end margin specify the **right** margin. In RTL direction, end margin specify the **left** margin.
915+
* **`marginHorizontal(_ value: CGFloat)`**
916+
Set the left, right, start and end margins to the specified value
917+
* **`marginVertical(_ value: CGFloat)`**
918+
Set the top and bottom margins to the specified value.
919+
* **`margin(_ value: CGFloat)`**
920+
Apply the value to all margins (top, left, bottom, right)
921+
* **`margin(_ insets: UIEdgeInsets)`**
922+
Set all margins using an UIEdgeInsets. This method is particularly useful to set all margins using iOS 11 `UIView.safeAreaInsets`.
923+
* **`margin(_ vertical: CGFloat, _ horizontal: CGFloat)`**
924+
Set the individually vertical margins (top, bottom) and horizontal margins (left, right, start, end)
925+
* **`margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat)`**
926+
Set individually top, horizontal margins and bottom margin
927+
* **`margin(_ top: CGFloat, _ right: CGFloat, _ bottom: CGFloat, _ left: CGFloat)`**
919928

920929
###### Usage examples:
921930
```swift

Sources/PinLayout.swift

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -286,45 +286,89 @@ public protocol PinLayout {
286286
//
287287
// MARK: Margins
288288
//
289+
290+
/**
291+
Set the top margin.
292+
*/
289293
@discardableResult func marginTop(_ value: CGFloat) -> PinLayout
294+
295+
/**
296+
Set the left margin.
297+
*/
290298
@discardableResult func marginLeft(_ value: CGFloat) -> PinLayout
299+
300+
/**
301+
Set the bottom margin.
302+
*/
291303
@discardableResult func marginBottom(_ value: CGFloat) -> PinLayout
304+
305+
/**
306+
Set the right margin.
307+
*/
292308
@discardableResult func marginRight(_ value: CGFloat) -> PinLayout
309+
293310
// RTL support
311+
/**
312+
Set the start margin.
313+
314+
Depends on the value of `Pin.layoutDirection(...)`:
315+
* In LTR direction, start margin specify the **left** margin.
316+
* In RTL direction, start margin specify the **right** margin.
317+
*/
294318
@discardableResult func marginStart(_ value: CGFloat) -> PinLayout
319+
320+
/**
321+
Set the end margin.
322+
323+
Depends on the value of `Pin.layoutDirection(...)`:
324+
* In LTR direction, end margin specify the **right** margin.
325+
* In RTL direction, end margin specify the **left** margin.
326+
*/
295327
@discardableResult func marginEnd(_ value: CGFloat) -> PinLayout
296328

329+
/**
330+
Set the left, right, start and end margins to the specified value.
331+
*/
297332
@discardableResult func marginHorizontal(_ value: CGFloat) -> PinLayout
333+
334+
/**
335+
Set the top and bottom margins to the specified value.
336+
*/
298337
@discardableResult func marginVertical(_ value: CGFloat) -> PinLayout
299338

300-
/// Apply to all four margins (top, left, bottom, right)
301-
///
302-
/// - Parameter value: <#value description#>
303-
/// - Returns: <#return value description#>
339+
/**
340+
Set all margins using UIEdgeInsets.
341+
This method is particularly useful to set all margins using iOS 11 `UIView.safeAreaInsets`.
342+
*/
343+
@discardableResult func margin(_ insets: UIEdgeInsets) -> PinLayout
344+
345+
/**
346+
Set margins using NSDirectionalEdgeInsets.
347+
This method is particularly to set all margins using iOS 11 `UIView.directionalLayoutMargins`.
348+
349+
Available only on iOS 11 and higher.
350+
*/
351+
//@available(iOS 11.0, *)
352+
//@discardableResult func margin(_ directionalInsets: NSDirectionalEdgeInsets) -> PinLayout
353+
354+
/**
355+
Set all margins to the specified value.
356+
*/
304357
@discardableResult func margin(_ value: CGFloat) -> PinLayout
305-
/// vertical | horizontal
306-
///
307-
/// - Parameters:
308-
/// - vertical: <#vertical description#>
309-
/// - horizontal: <#horizontal description#>
310-
/// - Returns: <#return value description#>
358+
359+
/**
360+
Set individually vertical margins (top, bottom) and horizontal margins (left, right, start, end).
361+
*/
311362
@discardableResult func margin(_ vertical: CGFloat, _ horizontal: CGFloat) -> PinLayout
312-
/// top | horizontal | bottom
313-
///
314-
/// - Parameters:
315-
/// - top: <#top description#>
316-
/// - horizontal: <#horizontal description#>
317-
/// - bottom: <#bottom description#>
318-
/// - Returns: <#return value description#>
363+
364+
/**
365+
Set individually top, horizontal margins and bottom margin.
366+
*/
319367
@discardableResult func margin(_ top: CGFloat, _ horizontal: CGFloat, _ bottom: CGFloat) -> PinLayout
320-
/// top | right | bottom | left
321-
///
322-
/// - Parameters:
323-
/// - top: <#top description#>
324-
/// - left: <#left description#>
325-
/// - bottom: <#bottom description#>
326-
/// - right: <#right description#>
327-
/// - Returns: <#return value description#>
368+
369+
/**
370+
Set individually top, left, bottom and right margins.
371+
*/
328372
@discardableResult func margin(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> PinLayout
329373

330374
/// Normally if only either left or right has been specified, PinLayout will MOVE the view to apply left or right margins.

Sources/PinLayoutImpl.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,25 @@ class PinLayoutImpl: PinLayout {
769769
marginBottom = value
770770
return self
771771
}
772+
773+
@discardableResult
774+
func margin(_ insets: UIEdgeInsets) -> PinLayout {
775+
marginTop = insets.top
776+
marginBottom = insets.bottom
777+
marginLeft = insets.left
778+
marginRight = insets.right
779+
return self
780+
}
781+
782+
/*@available(iOS 11.0, *)
783+
@discardableResult
784+
func margin(_ directionalInsets: NSDirectionalEdgeInsets) -> PinLayout {
785+
marginTop = directionalInsets.top
786+
marginBottom = directionalInsets.bottom
787+
marginStart(directionalInsets.leading)
788+
marginEnd(directionalInsets.trailing)
789+
return self
790+
}*/
772791

773792
@discardableResult
774793
func margin(_ value: CGFloat) -> PinLayout {

Tests/MarginsSpec.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,5 +547,35 @@ class MarginsSpec: QuickSpec {
547547
expect(aView.frame).to(equal(CGRect(x: 300, y: 340, width: 200.0, height: 120.0)))
548548
}
549549
}
550+
551+
//
552+
// margin using UIEdgeInsets and NSDirectionalEdgeInsets
553+
//
554+
describe("the result of top&bottom margins when the hCenter and vCenter are specified") {
555+
it("should adjust the aView") {
556+
aView.pin.top().bottom().left().right().margin(UIEdgeInsets(top: 10, left: 20, bottom: 30, right: 40))
557+
expect(aView.frame).to(equal(CGRect(x: 20.0, y: 10.0, width: 340.0, height: 360.0)))
558+
}
559+
560+
/*if #available(iOS 11.0, *) {
561+
it("should adjust the aView") {
562+
Pin.layoutDirection(.ltr)
563+
aView.pin.top().bottom().start().end().margin(NSDirectionalEdgeInsets(top: 10, leading: 20, bottom: 30, trailing: 40))
564+
expect(aView.frame).to(equal(CGRect(x: 20.0, y: 10.0, width: 340.0, height: 360.0)))
565+
}
566+
567+
it("should adjust the aView") {
568+
Pin.layoutDirection(.rtl)
569+
aView.pin.top().bottom().start().end().margin(NSDirectionalEdgeInsets(top: 10, leading: 20, bottom: 30, trailing: 40))
570+
expect(aView.frame).to(equal(CGRect(x: 40.0, y: 10.0, width: 340.0, height: 360.0)))
571+
}
572+
573+
it("should adjust the aView") {
574+
Pin.layoutDirection(.auto)
575+
aView.pin.top().bottom().start().end().margin(NSDirectionalEdgeInsets(top: 10, leading: 20, bottom: 30, trailing: 40))
576+
expect(aView.frame).to(equal(CGRect(x: 20.0, y: 10.0, width: 340.0, height: 360.0)))
577+
}
578+
}*/
579+
}
550580
}
551581
}

0 commit comments

Comments
 (0)