1

I want to draw the character "A" and then the character "B" exactly one inch apart, horizontally, on the screen of my MacBook pro. So far, I have found documentation that indicates an inch is 72.0 drawing units. The NSWindow also has a backingScaleFactor of 2.0 so there should actually be 144.0 units per inch. The problem is that when I draw these characters 144.0 units apart, they are more than an inch apart on the screen. They are roughly 1.1 inches apart.

I call this function with X coordinates of 500, 644, 788:

nsstring.draw( at: point, withAttributes: nil )

How can I calculate the scaling to get the characters and all the other elements of my drawing, to be accurately scaled so one inch in my document shows up as one inch on the screen?

David Rector
  • 958
  • 9
  • 31

1 Answers1

0

Thanks to HangarRash for finding it, the Swift code from this S.O. answer did the trick. I somehow missed it or ignored it because it was all about screens and not windows or views:

How to read the physical screen size of OSX?

I wonder what happens if a window spans two screens of different resolutions. I'll cross that bridge when I come to it if it's even a problem.

I added the NSScreen extension I got from that other question and then used a line of code to get the screen for my NSView:

let units = self.window?.screen?.unitsPerInch

It worked like a charm.

David Rector
  • 958
  • 9
  • 31