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
}