Swiftpack.co -  marchv/UIScreenExtension as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
marchv/UIScreenExtension
An extension to UIScreen that offers information about the pixel density (or point density) of iPhones and iPads.
.package(url: "https://github.com/marchv/UIScreenExtension.git", from: "0.0.9")

UIScreenExtension

This is an extension to UIScreen that provides information about the pixel density (or point density) of iPhones and iPads. For example this can be used to:

  • compute the dimension of the screen

  • draw a ruler; or

  • to show a proper ECG graph on the iPhone's screen.

    public extension UIScreen {
    
      /// The number of pixels per inch for this device
      public static let pixelsPerInch: CGFloat?
      
      /// The number of pixels per centimeter for this device
      public static let pixelsPerCentimeter: CGFloat?
      
      /// The number of points per inch for this device
      public static let pointsPerInch: CGFloat?
      
      /// The number of points per centimeter for this device
      public static let pointsPerCentimeter: CGFloat?
      
      /// The screen dimension in inches
      public static let dimensionInInches: CGSize?
      
      /// The screen dimension in centimeters
      public static let dimensionInCentimeters: CGSize?
    }
    

The constants will be set to .none if the device is unknown. This can happen if Apple releases a new iPhone, so please remember to deal with this in your code.

Installation and use

The extension can be installed using Swift Package Manager or Cocoapods by adding below line to your Podfile:

pod 'UIScreenExtension', :git => 'https://github.com/marchv/UIScreenExtension'

To use the extension in your code first import it by adding:

import UIScreenExtension

And then use it for example:

if let pointsPerCentimeter = UIScreen.pointsPerCentimeter {
   // code
}

Or to compute the diagonal screen dimension:

if let dimensionInInches = UIScreen.dimensionInInches {
    print(sqrt(pow(dimensionInInches.width, 2) + pow(dimensionInInches.height, 2))) // for example ~5.5 inches for iPhone 7 Plus
}

Support

The extension supports all iPhones, iPods and iPads that are supported by iOS 9-14.

Limitations

Please note that iPhone only apps upscaled on iPads will not work properly!

Improvements

  • Apple Watch support
  • Objective C support
  • Fetch information about new iPhone models from internet

Author

jens.schwarzer@marchv.com

:)

GitHub

link
Stars: 51
Last commit: 4 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Zoomed display support
10 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API