What is the currently recommended way to create a pdf document in Xcode 13 for Mac OSX using Swift and SwiftUI?
I find very little documentation on this. Most articles are for IOS. Only some articles for OSX from many, many years back. With SwiftUI, it will become common place for our code to be used interchangeable in multiple operating systems. And pdf is pretty much the way to go with document and report printouts.
Please point me in the right direction. Maybe I was just not looking in the right place.
My pdf documents are created without any problem for IOS and Ipad using CG and PDFKit functions.
But the same code did not work for the Mac app. So I modified the UIFont to NSFont, etc. Context functions are different. Moved to using CoreText for the attributed string placements. ( CTFrame )
But the strange stuff is the Y point placement that is correct for IOS but seems to be reversed for OSX.
So this was when I thought I must be using some old stuff that is not meant for today's work.
Brief Example below:
func pdf_placeText_CT( cgContext: CGContext, pageSize: CGSize, text: String, width: CGFloat, height: CGFloat, font: NSFont, alignment: String, xPoint: CGFloat, yPoint: CGFloat ) {
let pointY = pageSize.height - yPoint // for some reason we have to flip the vertical. Strange.
let paragraphstyle = NSMutableParagraphStyle()
if ( alignment == "R" ) {
paragraphstyle.alignment = .right
} else if ( alignment == "C" ) {
paragraphstyle.alignment = .center
} else {
paragraphstyle.alignment = .left
}
let attributedText = NSAttributedString(string: text, attributes: [
NSAttributedString.Key.font : font ,
NSAttributedString.Key.paragraphStyle : paragraphstyle
])
let stringSize = attributedText.size()
let stringRect = CGRect(x: xPoint, y: pointY, width: stringSize.width+5, height: stringSize.height+5)
let stringPath = CGPath(rect: stringRect, transform: nil)
//attributedText.draw(in: stringRect)
let textFrame = CTFramesetterCreateWithAttributedString(attributedText)
let textRange = CFRangeMake(0, attributedText.length)
var pageRange = CFRange()
CTFramesetterSuggestFrameSizeWithConstraints(textFrame, textRange, nil, pageSize, &pageRange)
let frame = CTFramesetterCreateFrame(textFrame, pageRange, stringPath, nil)
CTFrameDraw(frame, cgContext )
}
Post
Replies
Boosts
Views
Activity
How do you get the Maps in SwiftUI to show the User Location on the Apple Watch?
Map(coordinateRegion: $mapSetupRegion, interactionModes: mapInteractionModes, showsUserLocation: mapShowUserLocation, userTrackingMode: $mapTrackingMode )
These are the variables we have been using.
@State private var mapTrackingMode = MapUserTrackingMode.follow
private var mapShowUserLocation = true
private var mapInteractionModes = MapInteractionModes.all
@State private var mapSetupRegion = MKCoordinateRegion(
center: CLLocationCoordinate2D(
latitude : 37.3318,longitude : -122.0312),
span: MKCoordinateSpan( latitudeDelta : 0.1, longitudeDelta : 0.1 )
)
Thanks.
Having an issue with SwiftUI for IOS 14 that did not exist in 13 where when setting the variable for isPresented on the .sheet the value for the variable is not being passed on the first call. On second call the variable is passed correctly as True and the sheet is shown.
In other words, if you hit the button to call the sheet, the first time it shows Blank. Dismiss the sheet and then try it again, it shows with the correct body. Very odd.
Anyone else still having this issue. Working on a pretty large app where this was working fine before for the past 4 months. Now getting this type of behavior now that I have updated to Xcode 12 and IOS 14. Seeing this in both the Simulator and on the Ipad. Ipad is running 14.