ryanlintott / FrameUp

Reframing SwiftUI Views. A collection of tools to help with layout.
MIT License
214 stars 4 forks source link

Right to Left Language Support #5

Closed waelsaad closed 1 year ago

waelsaad commented 1 year ago

Hi,

I have closed my other issue to do with performance and would like your assistance please to let me know if FrameUp is already supporting justifying blocks of text for languages that are right to left.

I usually place the following 2 lines and it works but it didn't with the HFlow Layout.

Text(languageText)
          .multilineTextAlignment(.trailing)
          .frame(maxWidth: .infinity, alignment: .trailing)
    @ViewBuilder
    func justifiedItem(_ item: TextItem) -> some View {
        if let text = viewModel.getText(item) {
            let words = Array(text.string.split(separator: " ").map(String.init).enumerated())
            LazyVStack(spacing: 0) {
                HFlowLayout(alignment: .justified) {
                    ForEach(words, id: \.offset) { word in
                        Text(word.element)
                            .multilineTextAlignment(.trailing)
                            .frame(maxWidth: .infinity, alignment: .trailing)
                    }
                }
            }
        }
    }

I am happy to modify FrameUp myself if you guide me as to what can be done. I am hoping its simple enough to do. Thank you.

ryanlintott commented 1 year ago

Right to left layout is supported via the .layoutDirection environment variable. It will automatically change based on the user locale but you can also change it manually.

For example

HFlowLayout(alignment: alignment) {
  ForEach(items) { item in
      Text(item.value)
  }
}
.environment(\.layoutDirection, .rightToLeft)  /// manually set it here
ryanlintott commented 1 year ago

A couple more points:

ryanlintott commented 1 year ago

As right to left support is already included I'll mark this as closed.

waelsaad commented 1 year ago

Thank you Ryan, I appreciate your response. yes I have got the right to left working now. Amazing, and noted the other comments.