Hyphenation SwiftUI Text

Viewed 725

How can I add hyphenation to a SwiftUI Text? Currently SwiftUI makes a paragraph when there is no space, but this makes everything hard to read and messy.

enter image description here

I have done a bit of research and it seems that there is no native method. Is there another way to achieve this?

1 Answers

A possible approach is to add explicitly "soft hyphen" unicode symbols in places where you want hyphenation.

Here is a demo. Prepared with Xcode 12.4 / iOS 14.4

Case 1: enough space

Text("Minded­­­\u{AD}ness")
    .frame(width: 180)        // << wide frame
    .border(Color.black)

enter image description here

Case 2: limited space

Text("Minded­­­\u{AD}ness")
    .frame(width: 80)        // << limited frame
    .border(Color.black)

demo2

Note: Preview does not understand such unicode symbols, so test on Simulator or real device

Related