Post

Replies

Boosts

Views

Activity

Reply to .onAppear and .task code not running
Hi, Thanks for your response. I changed the original View names to generic names for the purpose of this post. The view variable is defined in the ContentView struct like this: @State var view: ScreenView ScreenView is an enum with the different view names. Only views 3 and 4 have code - the rest of the views/switch could be commented out.view6 is just the default view so there isn't a case for it in the switch statement. I changed .onAppear() to .onAppear. I don't see anything in the views that could possibly prevent those two functions from running.
Topic: Design SubTopic: General Tags:
Mar ’25
Reply to Rendering Multi-Page PDF
Thank you, I now have this: @MainActor func render() -> URL { let renderer = [ImageRenderer(content: pdfView())] let url = URL.documentsDirectory.appending(path: "output.pdf") var view = CGRect(x: 0, y: 0, width: 2550, height: 3300) guard let pdf = CGContext(url as CFURL, mediaBox: &view, nil) else { return nil // error } for image in renderer { image.render { size, context in pdf.beginPDFPage(nil) context(pdf) pdf.endPDFPage() } } pdf.closePDF() } I have two follow up questions: I'm getting an error shown above for returning a nil value but I'm not sure how to otherwise handle the else statement. I have a function pdfView that returns the view to be rendered. The syntax to return is -> some View { . Is it possible to return an array of views?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’25
Reply to Generating PDF SwiftUI
Here's what I've put together so far: let context = CGContext() let title = CTLineCreateWithAttributedString(CFAttributedString("Title")) One thing I don't understand is CGContext has many different of options as for parameters; some has width/height, some has other parameters. Which initializer would be ideal to use and what is the difference between setting the CTFrame (which I also don't know how to initialize) and the width/height of CGContext? I am also unsure as how to pass a string to CTLineCreateWithAttributedString. I tried this: let title = CTLineCreateWithAttributedString(CFAttributedString("Title")) Also, how do I map something like the CTLine to the context?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’25
Reply to Generating PDF SwiftUI
I found this: https://forums.developer.apple.com/forums/thread/671947 It explains a lot. I just have a few follow up questions: I'm getting errors with these two lines: context.setFillColor(NSColor.red.cgColor) context.addPath(shape.path(in: CGRect(origin: .zero, size: size))) Additionally, I see a lot of functions on the CGContext documentation page for text under the heading "Drawing Text" that does things such as setting the font, but I don't see a function that does the drawing. Also, there is code here that downloads a file of type FileDocument. Is there a way to convert the Data type (from the first link) generated with the code provided above to a FileDocument type? Could I also use a .fileExporter call to save the Data type or, is there a better way to save the file (for iOS)?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’25
Reply to Generating PDF SwiftUI
Hi, Thank you for your response however I'm not fully following the documentation you provided. I have a few questions: Listing 13-2 describes drawing a PDF page, but the function is declared with "void". What language is that? I searched CGContext and found many functions but I'm not sure how to use them. Would I have to create a CGContext object and call the functions on that? Is there a function for plain text? Listing 13-4 is a larger example, but I don't see any code that specifies the actual content being added to the PDF.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’25
Reply to Download Rendered PDF
Hi, Thank you for your response. I did not realize the code I provided was UIKit. I'd prefer to use SwiftUI. Would you be able to provide a solution to do the following: Generate a PDF composed of data from a SwiftData class Allow the user to export the PDF to their file app
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’25
Reply to Files Missing after Creating Branch
I don't even see a .git folder in the root directory. I had at least 20-30 files. I don't quite remember all of the steps I took. What I remember is I added a file to one of the branches, and went to switch to see if it only added the file in one of the branches. After that, both ended up empty. I have a revision on my phone and simulator. Is there any way to recover the files with that?
Nov ’24
Reply to .onAppear and .task code not running
Hi, Thanks for your response. I changed the original View names to generic names for the purpose of this post. The view variable is defined in the ContentView struct like this: @State var view: ScreenView ScreenView is an enum with the different view names. Only views 3 and 4 have code - the rest of the views/switch could be commented out.view6 is just the default view so there isn't a case for it in the switch statement. I changed .onAppear() to .onAppear. I don't see anything in the views that could possibly prevent those two functions from running.
Topic: Design SubTopic: General Tags:
Replies
Boosts
Views
Activity
Mar ’25
Reply to Rendering Multi-Page PDF
Thank you, I now have this: @MainActor func render() -> URL { let renderer = [ImageRenderer(content: pdfView())] let url = URL.documentsDirectory.appending(path: "output.pdf") var view = CGRect(x: 0, y: 0, width: 2550, height: 3300) guard let pdf = CGContext(url as CFURL, mediaBox: &view, nil) else { return nil // error } for image in renderer { image.render { size, context in pdf.beginPDFPage(nil) context(pdf) pdf.endPDFPage() } } pdf.closePDF() } I have two follow up questions: I'm getting an error shown above for returning a nil value but I'm not sure how to otherwise handle the else statement. I have a function pdfView that returns the view to be rendered. The syntax to return is -> some View { . Is it possible to return an array of views?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Feb ’25
Reply to Generating PDF SwiftUI
Here's what I've put together so far: let context = CGContext() let title = CTLineCreateWithAttributedString(CFAttributedString("Title")) One thing I don't understand is CGContext has many different of options as for parameters; some has width/height, some has other parameters. Which initializer would be ideal to use and what is the difference between setting the CTFrame (which I also don't know how to initialize) and the width/height of CGContext? I am also unsure as how to pass a string to CTLineCreateWithAttributedString. I tried this: let title = CTLineCreateWithAttributedString(CFAttributedString("Title")) Also, how do I map something like the CTLine to the context?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’25
Reply to Generating PDF SwiftUI
I found this: https://forums.developer.apple.com/forums/thread/671947 It explains a lot. I just have a few follow up questions: I'm getting errors with these two lines: context.setFillColor(NSColor.red.cgColor) context.addPath(shape.path(in: CGRect(origin: .zero, size: size))) Additionally, I see a lot of functions on the CGContext documentation page for text under the heading "Drawing Text" that does things such as setting the font, but I don't see a function that does the drawing. Also, there is code here that downloads a file of type FileDocument. Is there a way to convert the Data type (from the first link) generated with the code provided above to a FileDocument type? Could I also use a .fileExporter call to save the Data type or, is there a better way to save the file (for iOS)?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’25
Reply to Generating PDF SwiftUI
Hi, Thank you for your response however I'm not fully following the documentation you provided. I have a few questions: Listing 13-2 describes drawing a PDF page, but the function is declared with "void". What language is that? I searched CGContext and found many functions but I'm not sure how to use them. Would I have to create a CGContext object and call the functions on that? Is there a function for plain text? Listing 13-4 is a larger example, but I don't see any code that specifies the actual content being added to the PDF.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’25
Reply to Download Rendered PDF
Hi, Thank you for your response. I did not realize the code I provided was UIKit. I'd prefer to use SwiftUI. Would you be able to provide a solution to do the following: Generate a PDF composed of data from a SwiftData class Allow the user to export the PDF to their file app
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’25
Reply to Files Missing after Creating Branch
I don't even see a .git folder in the root directory. I had at least 20-30 files. I don't quite remember all of the steps I took. What I remember is I added a file to one of the branches, and went to switch to see if it only added the file in one of the branches. After that, both ended up empty. I have a revision on my phone and simulator. Is there any way to recover the files with that?
Replies
Boosts
Views
Activity
Nov ’24