1

I have a string of text of Arabic and English words, I need to add attribute using NSMutableAttributedString and add paragraph styling with NSMutableParagraphStyle. The problem is paragraph style doesn't apply to the text and not sure why. First I am detecting Arabic characters and changing the color (it works), and then trying to change line spacing or alignment which is not working. Here is the code:

let arabicChars = try? NSRegularExpression(pattern: "[\\u0600-\\u06FF\\u0750-\\u077F\\u08A0-\\u08FF\\uFB50-\\uFDFF\\uFE70-\\uFEFF\\s]+", options: [])
arabicChars?.enumerateMatches(in: pureText, options: [], range: range) { (result, _, _) in
    if let subStringRange = result?.range(at: 0) {
        stringText.addAttribute(.foregroundColor, value: UIColor(.brown), range: subStringRange)
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .right
        paragraphStyle.lineSpacing = 1.2
        paragraphStyle.lineHeightMultiple = 1.2
        stringText.addAttribute(.paragraphStyle, value: paragraphStyle, range: subStringRange)
        stringText.addAttribute(.font, value: UIFont(name: "CustomFont", size: 16)!, range: subStringRange)
    }
    }

And then using the function in SwiftUI like this:

    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 15) {
                Text(word.word?.capitalized ?? "")
                    .font(.system(size: 32, weight: .bold, design: .serif))
            
                Text(AttributedString(applyAttributedStyle(word.meaning)))
            }
            .frame(maxWidth: .infinity, alignment: .leading)
        }
        .padding(.horizontal)
    }
}

Here is the image that shows current result (left) vs right result: enter image description here

Edit 1: It works fine with UIKit's UITextView but not working on SwiftUI's Text

Edit 2: I have fixed the issue using UIViewRepresentable with UITextView

Mc.Lover
  • 4,813
  • 9
  • 46
  • 80
  • Can you show an example of the text you are using and how it is currently being displayed? Are you using the attributed string in a label or text view? Maybe the string contains some special RTL or LTR codes due to the mixed languages and that could be messing with the alignment. – HangarRash Feb 27 '23 at 23:10
  • Can you include a picture of the result? – HangarRash Feb 27 '23 at 23:12
  • @HangarRash I added the image – Mc.Lover Feb 28 '23 at 08:04
  • I suspect that while the VStack is wide, the individual Text elements are only as wide as their content, does setting a frame on each Text help? – Tristan Burnside Feb 28 '23 at 08:11
  • @TristanBurnside No, it doesn't affect the alignment of attributed texts, it's only dealing with the frame of Text object – Mc.Lover Feb 28 '23 at 08:14

1 Answers1

0

The reason this is true is that the SwiftUI Text view does not support all AttributedString attributes.

In particular, it only supports the attributes defined in AttributeScopes.SwiftUIAttributes, which does not include .paragraphStyle.

Currently the only solution is dropping to UIKit/AppKit, as you found for yourself.

This is some useful background reading: https://dimillian.medium.com/swiftui-attributedstring-is-not-there-yet-63d49e9f9c16

Brandon Horst
  • 1,921
  • 16
  • 26