Search code examples
swiftuitextarabic

How to show long Arabic text in SwiftUI?


In the code below, when small Arabic texts are placed, everything is displayed well, but when the text is long, the letters are displayed separately.

Text("مرحبا هذا نص طويل ولكنه باللغة العربية فكيف يمكن عرضه بشكل صحيح")
            .font(.system(size: 16))
            .foregroundColor(Color.gray)
          }

At first I thought it was happening because of one of the characters, but then I realized it was happening because of the length of the text

Text


Solution

  • This is a long-standing bug in Core Text, which SwiftUI is likely using behind the scenes. See CoreText With Too Long Arabic Text for this in a Core Text context. I do not believe this is solvable inside of SwiftUI. You need to create your own UIView (via UIViewRepresentable) and lay this out directly with a CTFramesetter. I'd check if it happens to be right around 10k of text. That's what we found in Core Text, and I expect it is the same in SwiftUI.