TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to support TextAlignment.natural in SwiftUI

Forums > SwiftUI

@X901  

Hi,

My app suppourt RTL and LTR text , like in Twitter I want the text to change depend on its content inside List if the text written RTL it should be to Right, but if it written in LTR language it should be Left show every cell will be diffrent depend on content language in UIKit I will use TextAlignment.natural and it will work fine but in SwiftUI I cannot make it work

I tried to create UILabel and UITextView everything is working fine but it TextAlignment.natural won't work

For testing purpose

RTL : هذا النص هو مثال لنص يمكن أن يستبدل في نفس المساحة، لقد تم توليد هذا النص من مولد النص العربى، حيث يمكنك أن تولد مثل هذا النص أو العديد من النصوص الأخرى إضافة إلى زيادة عدد الحروف التى يولدها التطبيق

LTR: Back to the Future will live forever in the minds of fans everywhere. The Hollywood classic touched generations and continues to be instantly recognizable.

struct LabelAlignment: UIViewRepresentable {
    var text: String
    var textAlignmentStyle : TextAlignmentStyle
    var width: CGFloat
    var fontSize: CGFloat
    var textColorName: String

    func makeUIView(context: Context) -> UILabel {
        let label = UILabel()
        label.textAlignment = NSTextAlignment(rawValue: textAlignmentStyle.rawValue)!
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = width
        label.setContentHuggingPriority(.required, for: .horizontal)
        label.setContentHuggingPriority(.required, for: .vertical)
        label.textColor = UIColor(named: textColorName)
        return label
    }

    func updateUIView(_ uiView: UILabel, context: Context) {
        uiView.text = text

        let font = UIFont.systemFont(ofSize: fontSize)
        uiView.font =  UIFontMetrics(forTextStyle: .headline).scaledFont(for: font)
        uiView.adjustsFontForContentSizeCategory = true

    }

}

enum TextAlignmentStyle : Int{
    case left = 0 ,center = 1 , right = 2 ,justified = 3 ,natural = 4

}

3      

@X901  

Is there anybody know how to fix this issue?

when using it with UIKit it need to use constraints on everyside to make it work https://www.hackingwithswift.com/example-code/uikit/how-to-support-right-to-left-languages

I tried it but it won't work

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.