Skip to content

Commit

Permalink
Support RTL languages in HStackView + activation constrains by scope (#3
Browse files Browse the repository at this point in the history
)

* added support rtl languages in HStackView

* formatted code

* added file names to header

* added activation constrains by scope

* migrated to swift 5.7

* simplified snapSubview method

* fixed type in constraints variable

* returned set to snapSubview method
  • Loading branch information
mezhevikin authored Nov 5, 2022
1 parent 326a934 commit 3715de1
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 102 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// swift-tools-version:5.3
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SwiftyUIKit",
platforms: [
.iOS(.v9)
.iOS(.v11)
],
products: [
.library(
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ or
}

```

#### Right-to-left languages
HStackView supports right-to-left languages by default.
Use `semanticContentAttribute = .forceLeftToRight` if you want to force direction.

## Installation

Expand Down
52 changes: 15 additions & 37 deletions Sources/SwiftyUIKit/HStackView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// HStackView.swift
//
//
// Created by Sergey Kazakov on 30.11.2020.
Expand All @@ -15,66 +15,44 @@ public extension HStackView {
convenience init(spacing: CGFloat = 0, @OptionalViewContentBuilder optionalContent: () -> [UIView]) {
self.init(spacing: spacing, content: optionalContent())
}

}

public final class HStackView: UIView {
public init(spacing: CGFloat = 0,
content: [UIView]) {
super.init(frame: .zero)
var constraints = [NSLayoutConstraint]()
translatesAutoresizingMaskIntoConstraints = false
content
.enumerated()
.forEach { (index, view) in
let isFirst = index == 0
let isLast = index == content.count - 1
let previousView: UIView? = isFirst ? nil : content[index - 1]
let prevView: UIView? = isFirst ? nil : content[index - 1]

view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)

view.topAnchor
.constraint(equalTo: topAnchor)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}

view.bottomAnchor
.constraint(equalTo: bottomAnchor)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}

constraints.append(view.topAnchor.constraint(equalTo: topAnchor))
constraints.append(view.bottomAnchor.constraint(equalTo: bottomAnchor))

if isFirst {
view.leftAnchor
.constraint(equalTo: leftAnchor)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}
constraints.append(view.leadingAnchor.constraint(equalTo: leadingAnchor))
}

if let prevView = previousView {
view.leftAnchor
.constraint(equalTo: prevView.rightAnchor,
constant: spacing)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}
if let prevView {
constraints.append(view.leadingAnchor.constraint(
equalTo: prevView.trailingAnchor,
constant: spacing))
}

if isLast {
view.rightAnchor
.constraint(equalTo: rightAnchor)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}
constraints.append(view.trailingAnchor.constraint(equalTo: trailingAnchor))
}
}

constraints.forEach { $0.priority = .defaultHigh }
NSLayoutConstraint.activate(constraints)
}

required init?(coder: NSCoder) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyUIKit/Modifiable.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// Modifiable.swift
//
//
// Created by Sergey Kazakov on 01.12.2020.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyUIKit/SpacerView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// SpacerView.swift
//
//
// Created by Sergey Kazakov on 30.11.2020.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyUIKit/UIView+Padding.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// UIView+Padding.swift
//
//
// Created by Sergey Kazakov on 22.09.2020.
Expand Down
28 changes: 9 additions & 19 deletions Sources/SwiftyUIKit/UIView+Snap.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// UIView+Snap.swift
//
//
// Created by Sergey Kazakov on 30.11.2020.
Expand All @@ -15,16 +15,16 @@ public extension UIView {

internal extension UIView {
enum FlexibleEdge: Int, Equatable, CaseIterable {
case top
case left
case right
case bottom
}
case top
case left
case right
case bottom
}
}

internal extension UIView {
func snapSubview(_ subview: UIView,
flexibleEdges: [FlexibleEdge]? = nil,
flexibleEdges: [FlexibleEdge] = [],
center: Bool = false) {
addSubview(subview)
subview.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -35,19 +35,9 @@ internal extension UIView {
subview.centerXAnchor.constraint(equalTo: centerXAnchor)
])
}

guard let flexibleEdges = flexibleEdges else {
NSLayoutConstraint.activate([
subview.leftAnchor.constraint(equalTo: leftAnchor),
subview.rightAnchor.constraint(equalTo: rightAnchor),
subview.topAnchor.constraint(equalTo: topAnchor),
subview.bottomAnchor.constraint(equalTo: bottomAnchor)
])
return
}


let edges = Set(flexibleEdges)

NSLayoutConstraint.activate([
edges.contains(.left) ?
subview.leftAnchor.constraint(greaterThanOrEqualTo: leftAnchor)
Expand Down
54 changes: 16 additions & 38 deletions Sources/SwiftyUIKit/VStackView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// VStackView.swift
//
//
// Created by Sergey Kazakov on 30.11.2020.
Expand All @@ -21,60 +21,38 @@ public final class VStackView: UIView {
public init(spacing: CGFloat = 0,
content: [UIView]) {
super.init(frame: .zero)

var constraints = [NSLayoutConstraint]()
translatesAutoresizingMaskIntoConstraints = false
content
.enumerated()
.forEach { (index, view) in
let isFirst = index == 0
let isLast = index == content.count - 1
let previousView: UIView? = isFirst ? nil : content[index - 1]
let prevView: UIView? = isFirst ? nil : content[index - 1]

view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)

view.leftAnchor
.constraint(equalTo: leftAnchor)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}

view.rightAnchor
.constraint(equalTo: rightAnchor)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}


constraints.append(view.leftAnchor.constraint(equalTo: leftAnchor))
constraints.append(view.rightAnchor.constraint(equalTo: rightAnchor))

if isFirst {
view.topAnchor
.constraint(equalTo: topAnchor)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}
constraints.append(view.topAnchor.constraint(equalTo: topAnchor))
}

if let prevView = previousView {
view.topAnchor
.constraint(equalTo: prevView.bottomAnchor,
constant: spacing)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}
if let prevView {
constraints.append(view.topAnchor.constraint(
equalTo: prevView.bottomAnchor,
constant: spacing))
}

if isLast {
view.bottomAnchor
.constraint(equalTo: bottomAnchor)
.modify {
$0.priority = .defaultHigh
$0.isActive = true
}
constraints.append(view.bottomAnchor.constraint(equalTo: bottomAnchor))
}
}

constraints.forEach { $0.priority = .defaultHigh }
NSLayoutConstraint.activate(constraints)
}

required init?(coder: NSCoder) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyUIKit/ViewContentBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// ViewContentBuilder.swift
//
//
// Created by Sergey Kazakov on 30.11.2020.
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftyUIKit/ZStackView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// ZStackView.swift
//
//
// Created by Sergey Kazakov on 30.11.2020.
Expand Down Expand Up @@ -43,7 +43,7 @@ public final class ZStackView: UIView {
case .bottom:
snapSubview($0, flexibleEdges: [.top])
case .all:
snapSubview($0, flexibleEdges: nil)
snapSubview($0)
case .center:
snapSubview($0, flexibleEdges: FlexibleEdge.allCases, center: true)
case .vertical:
Expand Down

0 comments on commit 3715de1

Please sign in to comment.