Skip to content

Commit 720ae93

Browse files
committed
Implement multilineTextAlignment modifier (for GtkBackend and AppKitBackend)
1 parent 2bcbb78 commit 720ae93

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,10 @@ public final class AppKitBackend: AppBackend {
303303
width: (proposedFrame?.x).map(CGFloat.init) ?? 0,
304304
height: .greatestFiniteMagnitude
305305
)
306-
let font = Self.font(for: environment)
307306
let rect = NSString(string: text).boundingRect(
308307
with: proposedSize,
309308
options: [.usesLineFragmentOrigin],
310-
attributes: [.font: font]
309+
attributes: Self.attributes(forTextIn: environment)
311310
)
312311
return SIMD2(
313312
Int(rect.size.width.rounded(.awayFromZero)),
@@ -644,13 +643,30 @@ public final class AppKitBackend: AppBackend {
644643
) -> NSAttributedString {
645644
NSAttributedString(
646645
string: text,
647-
attributes: [
648-
.foregroundColor: environment.foregroundColor.nsColor,
649-
.font: font(for: environment),
650-
]
646+
attributes: attributes(forTextIn: environment)
651647
)
652648
}
653649

650+
private static func attributes(
651+
forTextIn environment: Environment
652+
) -> [NSAttributedString.Key: Any] {
653+
let paragraphStyle = NSMutableParagraphStyle()
654+
paragraphStyle.alignment =
655+
switch environment.multilineTextAlignment {
656+
case .leading:
657+
.left
658+
case .center:
659+
.center
660+
case .trailing:
661+
.right
662+
}
663+
return [
664+
.foregroundColor: environment.foregroundColor.nsColor,
665+
.font: font(for: environment),
666+
.paragraphStyle: paragraphStyle,
667+
]
668+
}
669+
654670
private static func font(for environment: Environment) -> NSFont {
655671
switch environment.font {
656672
case .system(let size, let weight, let design):

Sources/GtkBackend/GtkBackend.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,15 @@ public final class GtkBackend: AppBackend {
278278
textView.label = content
279279
textView.wrap = true
280280
textView.lineWrapMode = .wordCharacter
281+
textView.justify =
282+
switch environment.multilineTextAlignment {
283+
case .leading:
284+
Justification.left
285+
case .center:
286+
Justification.center
287+
case .trailing:
288+
Justification.right
289+
}
281290

282291
textView.css.clear()
283292
textView.css.set(properties: Self.cssProperties(for: environment))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extension View {
2+
/// Sets the alignment of lines of text relative to each other in multiline
3+
/// text views.
4+
public func multilineTextAlignment(_ alignment: HorizontalAlignment) -> some View {
5+
return EnvironmentModifier(self) { environment in
6+
return environment.with(\.multilineTextAlignment, alignment)
7+
}
8+
}
9+
}

Sources/SwiftCrossUI/ViewGraph/Environment.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public struct Environment {
55
public var layoutSpacing: Int
66
public var foregroundColor: Color
77
public var font: Font
8+
public var multilineTextAlignment: HorizontalAlignment
89

910
init() {
1011
onResize = { _ in }
@@ -13,6 +14,7 @@ public struct Environment {
1314
layoutSpacing = 10
1415
foregroundColor = .black
1516
font = .system(size: 12)
17+
multilineTextAlignment = .leading
1618
}
1719

1820
public func with<T>(_ keyPath: WritableKeyPath<Self, T>, _ newValue: T) -> Self {

0 commit comments

Comments
 (0)