How to show HTML or Markdown in a SwiftUI Text?

iOS 15 Text now supports basic Markdown! struct ContentView: View { var body: some View { VStack { Text(“Regular”) Text(“*Italics*”) Text(“**Bold**”) Text(“~Strikethrough~”) Text(“`Code`”) Text(“[Link](https://apple.com)”) Text(“***[They](https://apple.com) ~are~ `combinable`***”) } } } Result: Update: If you store markdown as a String, it won’t render — instead, set the type to be LocalizedStringKey. struct ContentView: View { @State … Read more

@Published property wrapper not working on subclass of ObservableObject

Finally figured out a solution/workaround to this issue. If you remove the property wrapper from the subclass, and call the baseclass objectWillChange.send() on the variable the state is updated properly. NOTE: Do not redeclare let objectWillChange = PassthroughSubject<Void, Never>() on the subclass as that will again cause the state not to update properly. I hope … Read more

How to configure ContextMenu buttons for delete and disabled in SwiftUI?

All of the asked situations are now supported in iOS 15 Destructive: (works from iOS 15) Set .destructive as the role argument of the button: Button(role: .destructive) { // 👈 This argument // delete something } label: { Label(“Delete”, systemImage: “trash”) } Disabled: (works from iOS 14.2) Add .disabled modifier to the button. Button { … Read more

Get width of a view using in SwiftUI

The only available mechanism to get the dimension of a view, that is auto-resized by SwiftUI, is the GeometryReader. The GeometryReader is a proxy view that returns the dimensions of the container in which your view gets rendered. struct SomeView: View { @State var size: CGSize = .zero var body: some View { VStack { … Read more

How to make view the size of another view in SwiftUI

I have written a detailed explanation about using GeometryReader, view preferences and anchor preferences. The code below uses those concepts. For further information on how they work, check this article I posted: https://swiftui-lab.com/communicating-with-the-view-tree-part-1/ The solution below, will properly animate the underline: I struggled to make this work and I agree with you. Sometimes, you just … Read more

SwiftUI View – viewDidLoad()?

We can achieve this using view modifier. Create ViewModifier: struct ViewDidLoadModifier: ViewModifier { @State private var didLoad = false private let action: (() -> Void)? init(perform action: (() -> Void)? = nil) { self.action = action } func body(content: Content) -> some View { content.onAppear { if didLoad == false { didLoad = true action?() … Read more

SwiftUI View updating

You just have to observe the object at the appropriate level. Each @Published only triggers a refresh if the object as a whole has changed. In you example the array will change if you replace the array or add/remove objects. import SwiftUI struct ExerciseProgramView: View { //At this level you will see the entire program … Read more