gpt4 book ai didi

ios - SwiftUI View 无法正确呈现为UIImage

转载 作者:行者123 更新时间:2023-11-30 10:24:18 24 4
gpt4 key购买 nike

由于我定义的方法,这可能是一个非常小众的问题,但希望有人可以帮助发现此代码中的错误。

因此,一方面,我将Multiline TextField定义为:

struct MultilineTextField: View {

@State var dynamicHeight: CGFloat = 60

@Binding var text: String

var body: some View {
UITextViewWrapper(text: $text, calculatedHeight: $dynamicHeight)
.frame(height: dynamicHeight)
}

}


fileprivate struct UITextViewWrapper: UIViewRepresentable {

@Binding var text: String
@Binding var calculatedHeight: CGFloat

func makeUIView(context: Context) -> UITextView {
let textField = UITextView()

textField.delegate = context.coordinator
textField.isEditable = true
textField.font = UIFont.preferredFont(forTextStyle: .headline)
textField.isSelectable = true
textField.isUserInteractionEnabled = true
textField.isScrollEnabled = false
textField.backgroundColor = UIColor.clear
textField.returnKeyType = .done
textField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
textField.textAlignment = .center
textField.textContainerInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

return textField
}

func updateUIView(_ uiView: UITextView, context: Context) {

uiView.text = self.text

UITextViewWrapper.recalculateHeight(view: uiView, result: $calculatedHeight)
}

fileprivate static func recalculateHeight(view: UIView, result: Binding<CGFloat>) {

let newSize = view.sizeThatFits(CGSize(width: view.frame.size.width, height: CGFloat.greatestFiniteMagnitude))

if result.wrappedValue != newSize.height {
DispatchQueue.main.async {
result.wrappedValue = newSize.height // !! must be called asynchronously
}
}
}

func makeCoordinator() -> Coordinator {
return Coordinator(text: $text, height: $calculatedHeight)
}

final class Coordinator: NSObject, UITextViewDelegate {
var text: Binding<String>
var calculatedHeight: Binding<CGFloat>

init(text: Binding<String>, height: Binding<CGFloat>) {
self.text = text
self.calculatedHeight = height
}

func textViewDidChange(_ uiView: UITextView) {
text.wrappedValue = uiView.text
UITextViewWrapper.recalculateHeight(view: uiView, result: calculatedHeight)
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText
return
}

}

}


然后,我有一种方法可以将SwiftUI视图转换为UIImage,定义为:

extension View {
func asImage() -> UIImage {
let controller = UIHostingController(rootView: self)

controller.view.frame = CGRect(x: 0, y: CGFloat(Int.max), width: 1, height: 1)
UIApplication.shared.windows.first!.rootViewController?.view.addSubview(controller.view)

let size = controller.sizeThatFits(in: UIScreen.main.bounds.size)
controller.view.bounds = CGRect(x: 0, y: 0, width: size.width, height: size.height)
controller.view.sizeToFit()

controller.view.backgroundColor = UIColor.white.withAlphaComponent(0.0)

let image = controller.view.asImage()
controller.view.removeFromSuperview()
return image
}
}

extension UIView {
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}


但是,当我尝试将MultilineTextField转换为UIImage时,MultilineTextField的高度始终不正确。无论里面有多少文本,它似乎始终默认为60。

为了进行比较,当我在其他SwiftUI视图中使用MultilineTextField时,高度可以正常工作和更新,并根据MultilineTextField内部的文本进行增加和减少。

我的假设是,当我尝试将MultilineTextField视图转换为UIView时,我没有正确创建一个虚拟的subView来添加它(出于将其捕获为UIImage的目的)。但是我真的不知道。

最佳答案

通常,我发现最好使用UIViewControllerRepresentable而不是UIViewRepresentable ...,在那里您将拥有更多的访问生命周期,可以调整大小。

关于ios - SwiftUI View 无法正确呈现为UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60159537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com