I'm trying to create a Notes-like experience in my SwiftUI app where users can:
- Add photos as inline attachments within a TextEditor
- Move these attachments around within the text (like they were text characters)
- Delete attachments
- Preview attachments in full screen
- Have the attachments persist with the text content
Similar to how Apple Notes handles attachments, where images become part of the text flow and can be manipulated like text characters.
Current approach: I've tried using a custom TextEditor with a separate array of attachments, but I'm struggling to make the attachments behave as inline elements that can be moved within the text.
RichTextEditor:
struct RichTextEditor: View {
@State var content: String = ""
@State private var isGalleryPresented = false
@State var pickedImage: PhotosPickerItem?
@State private var selectedImage: UIImage?
var body: some View {
TextEditor(text: $content)
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Button {
isGalleryPresented = true
} label: {
Label("Attached", systemImage: "paperclip")
}
}
}
.photosPicker(isPresented: $isGalleryPresented, selection: $pickedImage)
.onChange(of: pickedImage) { oldItem, newItem in
Task {
if let pickedImage = pickedImage,
let data = try? await pickedImage.loadTransferable(type: Data.self),
let loadedImage = UIImage(data: data) {
if let imageData = loadedImage.jpegData(compressionQuality: 0.7) {
Task {
await addPhoto(imageData)
}
}
}
}
}
}
private func addPhoto(_ imageData: Data) async {
await MainActor.run {
let photo = PhotoData(imageData: imageData)
content += "\n[image:\(photo.id)]"
}
}
}