Where would I draw CGPaths in SwiftUI - swiftui

Usually you would use a custom view and the draw(rect:) method within in it to draw paths but since I don't have UIViews (and don't want to use UIKit integration) where can I draw?

You would use the GeometryReader and the Path
return GeometryReader { geometry in
Path { path in
path.addPath(...)
}
}
Apple Developer Documentation
And a Swift Tutorial on that topic also by Apple

Related

What is the recommended way to convert between pixels and points in SwiftUI?

I want to display a QR code in SwiftUI. The code is generated as a CGImage via CIImage. I don't want to scale it to the full size available because if the scaling factor isn't an integer there may be fuzzy boundaries between the QR modules. So I need a way to convert between iOS display points which I can get with GeometryReader and physical points. I've found a few search "hits" about reading the screen scale from a UIView, but not how I can get this scale in SwiftUI.
There a few more hits which just say the scale is 3 on all modern iPhones, and as I'm targeting iOS 15+ I think I can safely assume it's always 3 for now, but what if Apple bring out even higher pixel densities in future?
You can get displayScale using the Environment property wrapper.
struct ContentView: View {
#Environment(\.displayScale) var displayScale
var body: some View {
Text("display scale: \(displayScale)")
}
}
Consult EnvironmentValues to what else SwiftUI provides in the “environment”.

How to add character spacing in SwiftUI after applying fonts and color style?

I want to add character spacing in the text of SwiftUI in iOS version iOS 14 and plus.
When I use a new modifier called as kerning and Tracking after applying fonts styling then it won't work. So any pointer will be appreciated.
kerning and tracking are available in earlier versions of iOS, but only if they're applied directly to Text views.
What's new in iOS 16 is the ability to apply the modifier to any view, and have it apply to any of its Text child views.
Note the iOS availability flags:
Text.tracking(_:) - iOS 13.0+
View.tracking(_:) - iOS 16.0+
So that means that this should work for you in iOS 14:
Text("My Sample Text")
.tracking(-0.1)
but this would not:
VStack {
Text("My Sample View")
Text("With two lines of text")
}
.tracking(-0.1)

SwiftUI: Status bar color for specific view

I have a specific view in my application where I need the status bar to be white (dark mode).
I've tried setting the .preferredColorScheme(.dark) but as the documentation mentions, that affects all the views in my window which is not what I'm looking for.
I've taken a look at this but it seems to be done using SceneDelgate which I am not making use of.
Is there any workaround to this?
For iOS 16, you can set .toolbarColorScheme for a specific View. Unfortunately, this is not supported on older iOS Versions. As a workaround that does not involve SceneDelegate, you could manipulate UIToolBar.appearance() directly with a new tint color. This has nothing to do with the preferred color scheme (e.g., light or dark mode), but could have the same effect.

Detect single/multi-finger gesture Swiftui

How to detect number of finger interaction with Gesture in SwiftUI Gesture. With UIKit we can find number of find it from
let numberOfTouches = panGestureRecognizer.numberOfTouches
Any thought?
I am able to find my solution using UIKit UIView. Actually made container view(UIVIewRepresentable) which implements the PanGesture. I have used this view to to hold SwiftUI view. I can easily find and able to handle Panning behaviour.

Qt set iOS screen orientation

Working on a cross platform app in Qt. I need to be able to force the screen orientation for a couple of pages. I've found a way to do this for Android, but I can't find anything for iOS.
Is there any way to force set the screen orientation in iOS?
Better yet, is there a single call that will set the screen orientation for any given mobile platform?
Thanks!
Have a look here:
https://doc.qt.io/qt-5/qml-qtquick-window-screen.html#orientation-attached-prop
You could write the Screen.orientation (or Screen.primaryOrientation) property into your own property and use the onChanged event handler to get a Signal, eg:
readonly property var screenOrientation: Screen.orientation
onScreenOrientationChanged: {
console.log("Orientation Changed!")
}