Post

Replies

Boosts

Views

Activity

Text right-to-left direction with mathematical text
Hi, I am writing a calculator app in Swift 5 and decided to support eastern asian numerals to make it a bit interesting. The numeric display itself works well in Arabic langauge setting, but I have a protocol view that reports the entered data and results. I do it in a Grid with two Text items, one for the input and one for the result. The result contains a numeric string in U0660... arabic numerals, and that looks fine. The input is a construct looking like Text(number1) + Text(" ") + Text(operation), so it goes into a single Grid cell. The standard version looks like this: but the arabic version shows the numbers and operation symbols in the wrong sequence: I guess that has something to do with the mathematical symbols such as + and = simply register as ltr Text and confuse the text layout. If I localize π, the result looks different: So my question would be: How do I force a fragment such as Text(" +") to go in rtl direction? Is this a SwiftUI topic or doesit go deeper? I am looking at the Text element now, but may be that is the wrong approach.
6
0
153
3w
iOS 18, XCode 16, SwiftUI display HTML resource file
So I have decided to write a Swift application... It is going to be a calculator app, and right now I have a version where the calculator itself works and looks nice on the iPhone 15 Pro and iPhone16 simulator, the only thing that is still sort of wobbly is the built-in user manual. I have decided to put the localized manuals as html files into the app, so using the app does not need any external resources and may be used in environments where network access is not expected to be available. The first version I did of this was to do this: struct ManualView: View { var body: some View { if let fileUrl: URL = Bundle.main.url(forResource: "Manual", withExtension: "html") { if let nsAttributedString = try? NSAttributedString(url: fileUrl, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil), let attributedString = try? AttributedString(nsAttributedString, including: \.uiKit) { Text(attributedString) } else { Text("<div>Helpfile missing.</div>") } } } } #Preview { ManualView() } The result is extremely ugly. So I was looking for other ways and stumbled upon WKWebView. Now after fiddling around a bit with this I managed to get the preview to display the file view, but when I try to integrate it into the overall UI and open it on the device, the view stays blank. I have seen a few tutorials in the web, but nothing really worked, and occasionally the preview even crashes for reasons that are not quite clear to me. I do not really understand how to correctly integrate this WKWebView into a surrounding ScrollView, when all I want to do is to display a static HTML text without any external links, javascript or other dynamic behaviour. What I have now is this: struct ManualView: View { var body: some View { ManualViewRep() } } struct ManualViewRep: UIViewRepresentable { func updateUIView(_ uiView: WKWebView, context: Context) { if let fileUrl: URL = Bundle.main.url(forResource: "Manual", withExtension: "html") { uiView.loadFileURL(fileUrl, allowingReadAccessTo: fileUrl) } } func makeUIView(context: Context) -> WKWebView { let webConfiguration = WKWebViewConfiguration() let webView = WKWebView(frame: .zero, configuration: webConfiguration) return webView } } #Preview { ManualView() } This works in the direct preview in xcode, but everything else stays blank. What am I missing? Loading the file directly in makeUIView does not change anything, adding a default text in an else branch in case fileUrl fails neither. I looked at some web resources, but can't really figure out how this WKUIDelegate and ViewController stuff is supposed to work within the SwiftUI framework, in case that those are needed. Are they? Is there a simple but complete demonstration of how to load an display a HTML resource anywhere? Is there a better approach than WKWebKit to achieve a good result with iOS >= 15?
2
0
236
Oct ’24