Post

Replies

Boosts

Views

Activity

Reply to How to implement Notes-style attachments in TextEditor with movable/deletable images?
I will need to use UITextView which works with NSAttributedString. To add an image inline, you will need to create an Attachment for NSAttribuedString. Here is a short example: // create an NSMutableAttributedString that we'll append everything to let fullString = NSMutableAttributedString(string: "Start of text") // create our NSTextAttachment let image1Attachment = NSTextAttachment() image1Attachment.image = UIImage(named: "awesomeIcon.png") // wrap the attachment in its own attributed string so we can append it let image1String = NSAttributedString(attachment: image1Attachment) // add the NSTextAttachment wrapper to our full string, then add some more text. fullString.append(image1String) fullString.append(NSAttributedString(string: "End of text"))
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’25
Reply to SwiftData preview sample code
I found some working solutions. This is how I generate the preview containter: let previewContainer: ModelContainer = { do { let container = try ModelContainer(for: Snippet.self, configurations: ModelConfiguration(isStoredInMemoryOnly: true)) Task { @MainActor in let context = container.mainContext let snip = Snippet.example2() let folder = Folder(name: "folder with favorite snippet") context.insert(folder) folder.snippets.append(Snippet(isFavorite: true, title: "favorite snippet")) // add test data here } return container } catch { fatalError("Failed to create container: \(error.localizedDescription)") } }() #Preview { FolderListView() .modelContainer(previewContainer) } Using view where I need to pass in a SwiftData object is more complex. I created a demo project with more examples. All previews are working for Xcode 15 https://github.com/gahntpo/SnippetBox-SwiftData
Oct ’23