使用 AttributedString SwiftUI 3 无法进行文本对齐

Bas*_*sil 0 nsattributedstring swift swiftui attributedstring

AttributedString我尝试使用SwiftUI 3 中的新功能

我想改变对齐方式,但它不起作用。其他任何事情都工作正常,例如我尝试使用属性字符串更改颜色,它确实有效!但我无能为力paragraphStyle

struct ContentView: View {
    var body: some View {
       
        ZStack {
            
            Color(uiColor: UIColor(red: 0.92, green: 0.92, blue: 0.92, alpha: 1.00)).ignoresSafeArea()
                        
            VStack {
                
                Text("""
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
""" ) { string in
                    let paragraphStyle = NSMutableParagraphStyle()
                    paragraphStyle.alignment = .right

                    string.paragraphStyle = paragraphStyle
                    
                    string.foregroundColor = .blue
                }
                    .padding()
                    .textSelection(.enabled)
                

                Spacer()
                
            }
            
        }
        
    }

}

extension Text {
init(_ string: String, configure: ((inout AttributedString) -> Void)) {
    var attributedString = AttributedString(string) /// create an `AttributedString`
    configure(&attributedString) /// configure using the closure
    self.init(attributedString) /// initialize a `Text`
}
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Emi*_*aez 5

paragraphStyle属性是在UIKit 作用域中定义的,而不是在 上定义的SwiftUI,因此您不能指望它能够工作。

另一种方法是使用修饰符.multilineTextAlignment(.trailing)