[英]SwiftUI - Using UIViewRepresentable to wrap view in order to use NSMutableAttributedString
So in order to use NSMutableAttributedString with SwiftUI i wrapped a view with UIViewRepresentable, the problem is that the layout breaks after as so:因此,为了将 NSMutableAttributedString 与 SwiftUI 一起使用,我用 UIViewRepresentable 包装了一个视图,问题是布局在这样之后中断:
this is the result with the usual Text():这是通常的 Text() 的结果:
This is my vm:这是我的虚拟机:
final class ViewModel: ObservableObject, CryptoViewControllerFactoryType {
var firstText: String
var font: Font
var color: Color
var image = "Test"
var attribute: NSMutableAttributedString
var attribute1: NSMutableAttributedString
var attribute2: NSMutableAttributedString
var attribute3: NSMutableAttributedString
@Published var count = 0
init() {
firstText = "gjhgjhgjhg"
color = .red
font = Font.custom("Orion-ExtraBold", size: 35)
attribute = NSMutableAttributedString(string: " ifjglfjgsnfdiggslifjglfjgsnfdiggslifjglfjgsnfdiggslifjglfjgsnfdiggslifjglfjgsnfdiggslifjglfjgsnfdiggsl", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14),NSAttributedString.Key.foregroundColor: UIColor.red])
attribute1 = NSMutableAttributedString(string: "ifjglfjgsnfdiggsl;dfgbslfkbg;skjgbsg", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 50),NSAttributedString.Key.foregroundColor: UIColor.red])
and this is the view:这是视图:
struct TestView: View {
@ObservedObject var viewModel: ViewModel
var body: some View {
ZStack {
ScrollView {
VStack {
Image(viewModel.image).resizable()
.scaledToFit()
.overlay(
Button(action: {
print(">> Trash Tapped")
//viewModel.dismiss
}) {
Image("closeBtn")
}.padding(.top, 30)
.padding(.trailing, 16),
alignment: .topTrailing
)
.padding(.bottom, 40)
// Text("sdfsdfsdfsdfsdsffdsfsfsgsg")
TextView(text: $viewModel.attribute)
.padding(.trailing, 30)
.padding(.leading, 30)
.padding(.bottom, 40)
.multilineTextAlignment(.trailing)
TextView(text: $viewModel.attribute1)
.disabled(true)
.fixedSize()
.padding(.trailing, 30)
.padding(.leading, 30)
.padding(.bottom, 40)
.multilineTextAlignment(.trailing)
}
.frame(alignment: .top)
}
.edgesIgnoringSafeArea(.all)
}
.navigationBarHidden(true)
}
} }
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView(viewModel: ViewModel())
}
} }
struct TextView: UIViewRepresentable {
@Binding var text: NSMutableAttributedString
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.textAlignment = .right
label.numberOfLines = 0
//label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
return label
}
func updateUIView(_ uiView: UILabel, context: Context) {
uiView.attributedText = text
}
} }
Is there a way to use it correctly?有没有正确使用的方法? Thanks in advance提前致谢
Starting from iOS 15 you can use the NSAttributedString
directly in Text
component using this initializer:从iOS 15开始,您可以使用此初始化程序直接在Text
组件中使用NSAttributedString
:
public init(_ attributedContent: AttributedString) // Text
and use this initializer to convert NSAttributedString
to AttributedString
并使用此初始化程序将NSAttributedString
转换为AttributedString
public init(_ nsStr: NSAttributedString) // AttributedString
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.