Skip to content

Commit

Permalink
Merge pull request #140 from freshOS/function_builders
Browse files Browse the repository at this point in the history
Function builders, CGFloat, api clean, UIStackViews
  • Loading branch information
s4cha authored Apr 5, 2020
2 parents 0f5a031 + 3d8cd72 commit 70ccf38
Show file tree
Hide file tree
Showing 28 changed files with 2,071 additions and 413 deletions.
12 changes: 4 additions & 8 deletions LoginExample/LoginStevia/LoginViewNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ class LoginViewNative: UIView {

convenience init() {
self.init(frame:CGRect.zero)
render()
}

func render() {

// View Hieararchy
// 01 - View Hieararchy
email.translatesAutoresizingMaskIntoConstraints = false
password.translatesAutoresizingMaskIntoConstraints = false
login.translatesAutoresizingMaskIntoConstraints = false
addSubview(email)
addSubview(password)
addSubview(login)

// Layout (using latest layoutAnchors)
// 02 - Layout (using latest layoutAnchors)
email.topAnchor.constraint(equalTo: topAnchor, constant: 100).isActive = true
email.leftAnchor.constraint(equalTo: leftAnchor, constant: 8).isActive = true
email.rightAnchor.constraint(equalTo: rightAnchor, constant: -8).isActive = true
Expand All @@ -45,7 +41,7 @@ class LoginViewNative: UIView {
login.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
login.heightAnchor.constraint(equalToConstant: 80).isActive = true

// Styling
// 03 - Styling
backgroundColor = .gray
email.borderStyle = .roundedRect
email.autocorrectionType = .no
Expand All @@ -58,7 +54,7 @@ class LoginViewNative: UIView {
password.returnKeyType = .done
login.backgroundColor = .lightGray

// Content
// 04 - Content
email.placeholder = "Email"
password.placeholder = "Password"
login.setTitle("Login", for: .normal)
Expand Down
36 changes: 18 additions & 18 deletions LoginExample/LoginStevia/LoginViewStevia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ class LoginViewStevia: UIView {
self.init(frame:CGRect.zero)
// Get injectionForXcode here : http://johnholdsworth.com/injection.html

// View Hierarchy
// 01 -View Hierarchy
// This essentially does `translatesAutoresizingMaskIntoConstraints = false`
// and `addSubsview()`. The neat benefit is that
// (`sv` calls can be nested which will visually show hierarchy ! )
sv(
email,
password,
// (`Subviews` calls can be nested which will visually show hierarchy ! )
subviews {
email
password
login
)
// Vertical + Horizontal Layout in one pass
}

// 02 - Vertical + Horizontal Layout in one pass
// With type-safe visual format
layout(
100,
|-email-| ~ 80,
8,
|-password-| ~ 80,
"",
|login| ~ 80,
layout {
100
|-email-| ~ 80
8
|-password-| ~ 80
>=20
|login| ~ 80
0
)
}

// ⛓ Chainable api
// email.top(100).fillHorizontally(m: 8).height(80)
Expand Down Expand Up @@ -70,7 +70,7 @@ class LoginViewStevia: UIView {
// login.Height == 80


// Styling 🎨
// 03 - Styling 🎨
backgroundColor = .gray
email.style(commonFieldStyle)
password.style(commonFieldStyle).style { f in
Expand All @@ -79,7 +79,7 @@ class LoginViewStevia: UIView {
}
login.backgroundColor = .lightGray

// Content 🖋
// 04 - Content 🖋
email.placeholder = "Email"
password.placeholder = "Password"
login.setTitle("Login", for: .normal)
Expand Down
6 changes: 3 additions & 3 deletions Sources/Stevia/Stevia+Alignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public func alignCenter(_ v1: UIView, with v2: UIView) {
```
*/
public func alignHorizontally(_ v1: UIView, with v2: UIView, offset: CGFloat = 0) {
public func alignHorizontally(_ v1: UIView, with v2: UIView, offset: Double = 0) {
align(.horizontal, v1: v1, with: v2, offset: offset)
}

Expand All @@ -133,7 +133,7 @@ public func alignHorizontally(_ v1: UIView, with v2: UIView, offset: CGFloat = 0
```
*/
public func alignVertically(_ v1: UIView, with v2: UIView, offset: CGFloat = 0) {
public func alignVertically(_ v1: UIView, with v2: UIView, offset: Double = 0) {
align(.vertical, v1: v1, with: v2, offset: offset)
}

Expand All @@ -148,7 +148,7 @@ private func align(_ axis: NSLayoutConstraint.Axis, views: [UIView]) {
}
}

private func align(_ axis: NSLayoutConstraint.Axis, v1: UIView, with v2: UIView, offset: CGFloat) {
func align(_ axis: NSLayoutConstraint.Axis, v1: UIView, with v2: UIView, offset: Double) {
if let spv = v1.superview {
let center: NSLayoutConstraint.Attribute = axis == .horizontal ? .centerY : .centerX
let c = constraint(item: v1, attribute: center, toItem: v2, constant: offset)
Expand Down
68 changes: 49 additions & 19 deletions Sources/Stevia/Stevia+Center.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,77 +28,107 @@ public extension UIView {
}
return self
}

/**
Centers the view horizontally (X axis) in its container.
```
button.centerHorizontally()
button.centerHorizontally(offset: 40)
```
- Returns: Itself, enabling chaining,
*/
@discardableResult
func centerHorizontally() -> Self {
func centerHorizontally(offset: Double = 0) -> Self {
if let spv = superview {
align(vertically: self, spv)
align(.vertical, v1: self, with: spv, offset: offset)
}
return self
}

/**
Centers the view horizontally (X axis) in its container.
```
button.centerHorizontally()
button.centerHorizontally(offset: 40)
```
- Returns: Itself, enabling chaining,
*/
@discardableResult
func centerHorizontally(offset: CGFloat) -> Self {
centerHorizontally(offset: Double(offset))
}

/**
Centers the view horizontally (X axis) in its container.
```
button.centerHorizontally()
button.centerHorizontally(offset: 40)
```
- Returns: Itself, enabling chaining,
*/
@discardableResult
func centerHorizontally(offset: Int) -> Self {
centerHorizontally(offset: Double(offset))
}

/**
Centers the view vertically (Y axis) in its container.
```
button.centerVertically()
button.centerVertically(offset: 40)
```
- Returns: Itself, enabling chaining,
*/
@discardableResult
func centerVertically() -> Self {
func centerVertically(offset: Double = 0) -> Self {
if let spv = superview {
align(horizontally: self, spv)
align(.horizontal, v1: self, with: spv, offset: offset)
}
return self
}

/**
Centers the view horizontally (X axis) in its container, with an offset
Centers the view vertically (Y axis) in its container.
```
button.centerHorizontally(40)
button.centerVertically()
button.centerVertically(offset: 40)
```
- Returns: Itself, enabling chaining,
*/
@discardableResult
func centerHorizontally(_ offset: CGFloat) -> Self {
if let spv = superview {
alignVertically(self, with: spv, offset: offset)
}
return self
func centerVertically(offset: CGFloat) -> Self {
centerVertically(offset: Double(offset))
}

/**
Centers the view vertically (Y axis) in its container, with an offset
Centers the view vertically (Y axis) in its container.
```
button.centerVertically(40)
button.centerVertically()
button.centerVertically(offset: 40)
```
- Returns: Itself, enabling chaining,
*/
@discardableResult
func centerVertically(_ offset: CGFloat) -> Self {
if let spv = superview {
alignHorizontally(self, with: spv, offset: offset)
}
return self
func centerVertically(offset: Int) -> Self {
centerVertically(offset: Double(offset))
}
}
#endif
10 changes: 5 additions & 5 deletions Sources/Stevia/Stevia+Constraints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public extension UIView {
relatedBy: NSLayoutConstraint.Relation = .equal,
toItem view2: AnyObject? = nil,
attribute attr2: NSLayoutConstraint.Attribute? = nil,
multiplier: CGFloat = 1,
constant: CGFloat = 0) -> NSLayoutConstraint {
multiplier: Double = 1,
constant: Double = 0) -> NSLayoutConstraint {
let c = constraint(
item: view1, attribute: attr1,
relatedBy: relatedBy,
Expand Down Expand Up @@ -74,12 +74,12 @@ func constraint(item view1: AnyObject,
relatedBy: NSLayoutConstraint.Relation = .equal,
toItem view2: AnyObject? = nil,
attribute attr2: NSLayoutConstraint.Attribute? = nil, // Not an attribute??
multiplier: CGFloat = 1,
constant: CGFloat = 0) -> NSLayoutConstraint {
multiplier: Double = 1,
constant: Double = 0) -> NSLayoutConstraint {
let c = NSLayoutConstraint(item: view1, attribute: attr1,
relatedBy: relatedBy,
toItem: view2, attribute: ((attr2 == nil) ? attr1 : attr2! ),
multiplier: multiplier, constant: constant)
multiplier: CGFloat(multiplier), constant: CGFloat(constant))
c.priority = UILayoutPriority(rawValue: UILayoutPriority.defaultHigh.rawValue + 1)
return c
}
Expand Down
Loading

0 comments on commit 70ccf38

Please sign in to comment.