Bidirectional Text Rendering Issue in Swift UILabel for Arabic

I'm encountering an issue displaying a large HTML string (over 11470 characters) in a UILabel. Specifically, the Arabic text within the string is rendering left-to-right instead of the correct right-to-left direction. I've provided a truncated version of the HTML string and the relevant code snippet below. I've tried setting the UILabel's text alignment to right, but this didn't resolve the issue. Could you please advise on how to resolve this bidirectional text rendering problem?

The results of the correct and incorrect approaches are shown in the image below.

Here's the relevant Swift code:

let labelView: UILabel = {
        let label = UILabel()
        label.textAlignment = .right
        label.translatesAutoresizingMaskIntoConstraints = false
        label.numberOfLines = 0
        label.semanticContentAttribute = .forceRightToLeft
        label.backgroundColor = .white
        label.lineBreakMode = .byWordWrapping
        return label
    }()

//Important!!
//It must exceed 11470 characters.
let htmlString = """
        <p style=\"text-align: center;\"><strong>İSTİÂZE</strong></p> <p>Nahl sûresindeki:</p>
    <p dir="rtl" lang="ar">  فَاِذَا قَرَاْتَ الْقُرْاٰنَ فَاسْتَعِذْ بِاللّٰهِ مِنَ الشَّيْطَانِ الرَّج۪يمِ </p>
    <p><strong>“</strong><strong>Kur’an okuyacağın zaman kovulmuş şeytandan hemen Allah’a sığın!</strong><strong>”</strong> (Nahl 16/98) emri gereğince Kur’ân-ı Kerîm okumaya başlarken:</p> <p  dir="rtl" lang="ar">اَعُوذُ بِاللّٰهِ مِنَ الشَّيْطَانِ الرَّج۪يمِ</p> <p><em>“Kovulmuş şeytandan Allah’a sığınırım” </em>deriz. Bu sözü söylemeye “istiâze<em>” denilir. “Eûzü”</em>, sığınırım, emân dilerim, yardım taleb ederim, gibi anlamlara gelir. It must exceed 11470 characters.</p>
“””

if let data = htmlString.data(using: .utf8) {
            let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
                .documentType: NSAttributedString.DocumentType.html,
                .characterEncoding: String.Encoding.utf8.rawValue
            ]
            do {
            
                let attributedString = try NSAttributedString(data: data, options: options, documentAttributes: nil)
                labelView.attributedText = attributedString
                
            } catch {
                print("HTML string işlenirken hata oluştu: \(error)")
            }
        }

I'm using iOS 18.2 and Swift 6. Any suggestions on how to correct the bidirectional text rendering?

Bidirectional Text Rendering Issue in Swift UILabel for Arabic
 
 
Q