简体   繁体   English

SwiftUI - 使用 UIViewRepresentable 包装视图以使用 NSMutableAttributedString

[英]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.

相关问题 UIViewRepresentable 隐藏在 SwiftUI 视图后面 - UIViewRepresentable hidden behind SwiftUI View 从 SwiftUI 中的 UIViewRepresentable 呈现视图 - Present view from UIViewRepresentable in SwiftUI SwiftUI:如何从 UIViewRepresentable 导航到 SwiftUI 视图 - SwiftUI: How to navigate from UIViewRepresentable to SwiftUI View SwiftUI - 从 UITableView (UIViewRepresentable) 导航到 View - SwiftUI - Navigate from UITableView (UIViewRepresentable) to View 通过 UIViewRepresentable 调整 SwiftUI 中的 UILabel 大小,如 Text 以换行多行 - Size a UILabel in SwiftUI via UIViewRepresentable like Text to wrap multiple lines 如何将 SwiftUI 视图放置在 UIKit (UIViewRepresentable) 视图之上? (错误或错误?) - How to place a SwiftUI view on top of a UIKit (UIViewRepresentable) view? (bug or mistake?) 带有 UIViewRepresentable 的 SwiftUI 自定义文本字段 ObservableObject 和推送视图的问题 - SwiftUI Custom TextField with UIViewRepresentable Issue with ObservableObject and pushed View iOS:在 UIViewRepresentable 和 View 之间进行 function 调用,SwiftUI - iOS: Make function calls between UIViewRepresentable and View both ways, SwiftUI SwiftUI:避免使用 MKMapView+UIViewRepresentable 在 TabView 中重新创建/重新渲染视图 - SwiftUI: avoid recreating/rerendering view in TabView with MKMapView+UIViewRepresentable NSMutableAttributedString 的自动换行 - Word wrap for NSMutableAttributedString
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM