Lukas Anda
09/12/2023, 2:20 PMmohamed rejeb
09/12/2023, 2:38 PMLukas Anda
09/12/2023, 3:17 PMLukas Anda
09/12/2023, 3:45 PMAngga Ardinata
09/12/2023, 9:52 PMNvm, got it working with some dark magic hahamine sharing how to do it ?
Lukas Anda
09/13/2023, 6:41 AM@OptIn(ExperimentalForeignApi::class)
@Composable
internal actual fun PlatformText(text: String) {
val factory = LocalTestViewInterfaceFactory.current
var width by remember { mutableStateOf(0) }
var height by remember { mutableStateOf(0) }
LaunchedEffect(text) {
factory.showText(text)
}
UIKitView(
factory = {
factory.getView()
},
modifier = Modifier
.then(if (height == 0) Modifier else Modifier.height(height.dp))
.then(if (width == 0) Modifier else Modifier.width(width.dp))
,
onResize = { view, _ ->
view.frame.useContents {
height = this.size.height.toInt()
width = this.size.width.toInt()
}
}
)
}
And as for the view, I am still experimenting as the Swiftui is still complaining about environment used incorrectly but for the simple Text() it works fine (Replace LaTeX with Text):
struct TestView: View {
var body: some View {
VStack(alignment: .leading) {
LaTeX("The quadratic formula is $$x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}$$ and it has zeros at the roots of $f(x)=ax^2+bx+c$.")
.font(.system(size: 12))
.parsingMode(.onlyEquations)
.frame(maxWidth:.infinity, alignment: .leading)
.blockMode(.alwaysInline)
.background(.green)
}
.frame(alignment: .leading)
.frame(maxWidth: .infinity)
.background(.red)
}
}
and in UIKit:
class TestViewInterfaceImpl: TestViewInterface {
func getView() -> UIView {
let controller = UIHostingController(rootView: TestView())
let targetSize = controller.sizeThatFits(in: CGSize(width: UIScreen.main.bounds.width, height:UIScreen.main.bounds.height))
controller.view.frame = CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width, height:targetSize.height))
let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width, height:targetSize.height)))
view.addSubview(controller.view)
return view
}
func showText(text: String) {
}
}