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)