1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift:1mmを取得する

1
Last updated at Posted at 2018-08-08

2019/07/13更新

var millimeter: CGFloat {
    if let screen = self.view.window?.screen {
        let key = NSDeviceDescriptionKey(rawValue: "NSScreenNumber")
        let id = screen.deviceDescription[key] as! UInt32
        let physicalSize: CGSize = CGDisplayScreenSize(id)
        let pixelSize = screen.deviceDescription[NSDeviceDescriptionKey.size] as! CGSize
        return pixelSize.width / physicalSize.width
    } else {
        return 5.039370154423904 // According to MacBook Retina 13inch Default Resolution
    }
}

MacBookのディスプレイなら正しく1mmが取れるが,外部ディスプレイの場合正しく取れない場合もある模様.

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?