Skip to content

Commit 830e492

Browse files
committed
It is now possible to set TextAlignment using .multilineTextAlignment(_:) (read once on setup, à la SwiftUI.TextField) or proxy.text.alignment(_:).
1 parent 9abca2b commit 830e492

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

Sources/DiffableTextViewsXiOS/DiffableTextField.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public struct DiffableTextField<Style: DiffableTextStyleXiOS>: UIViewRepresentab
5959
let uiView = BasicTextField()
6060
uiView.delegate = context.coordinator
6161
uiView.font = UIFont(DiffableTextFont.body.monospaced())
62+
uiView.setTextAlignment(context.environment)
6263
uiView.setContentHuggingPriority(.defaultHigh, for: .vertical)
6364
uiView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
6465
//=--------------------------------------=
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//=----------------------------------------------------------------------------=
2+
// This source file is part of the DiffableTextViews open source project.
3+
//
4+
// Copyright (c) 2022 Oscar Byström Ericsson
5+
// Licensed under Apache License, Version 2.0
6+
//
7+
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
8+
//=----------------------------------------------------------------------------=
9+
10+
#if canImport(UIKit)
11+
12+
import UIKit
13+
14+
//*============================================================================*
15+
// MARK: * UIView
16+
//*============================================================================*
17+
18+
extension UIView {
19+
20+
//=------------------------------------------------------------------------=
21+
// MARK: Accessors
22+
//=------------------------------------------------------------------------=
23+
24+
@inlinable var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection {
25+
UIView.userInterfaceLayoutDirection(for: semanticContentAttribute)
26+
}
27+
}
28+
29+
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//=----------------------------------------------------------------------------=
2+
// This source file is part of the DiffableTextViews open source project.
3+
//
4+
// Copyright (c) 2022 Oscar Byström Ericsson
5+
// Licensed under Apache License, Version 2.0
6+
//
7+
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
8+
//=----------------------------------------------------------------------------=
9+
10+
#if canImport(UIKit)
11+
12+
import SwiftUI
13+
import UIKit
14+
15+
//*============================================================================*
16+
// MARK: * UITextField x Alignment
17+
//*============================================================================*
18+
19+
extension UITextField {
20+
21+
//=------------------------------------------------------------------------=
22+
// MARK: Transformations
23+
//=------------------------------------------------------------------------=
24+
25+
@inlinable func setTextAlignment(_ environment: EnvironmentValues) {
26+
self.setTextAlignment(environment.multilineTextAlignment)
27+
}
28+
29+
@inlinable func setTextAlignment(_ newValue: TextAlignment) {
30+
switch newValue {
31+
case .leading: textAlignment = (userInterfaceLayoutDirection == .leftToRight) ? .left : .right
32+
case .trailing: textAlignment = (userInterfaceLayoutDirection == .leftToRight) ? .right : .left
33+
case .center: textAlignment = .center
34+
}
35+
}
36+
}
37+
38+
#endif

Sources/DiffableTextViewsXiOS/Views/ProxyTextField+Text.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#if canImport(UIKit)
1111

12+
import SwiftUI
1213
import UIKit
1314

1415
//*============================================================================*
@@ -29,6 +30,10 @@ public extension ProxyTextField.Text {
2930
// MARK: Transformations
3031
//=------------------------------------------------------------------------=
3132

33+
@inlinable func alignment(_ alignment: TextAlignment) {
34+
wrapped.setTextAlignment(alignment)
35+
}
36+
3237
@inlinable func color(_ color: UIColor) {
3338
wrapped.textColor = color
3439
}

0 commit comments

Comments
 (0)