Swiftpack.co - marchv/UIScreenExtension as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by marchv.
marchv/UIScreenExtension 0.0.11
An extension to UIScreen that offers information about the pixel density (or point density) of iPhones and iPads.
⭐️ 70
🕓 28 weeks ago
iOS
.package(url: "https://github.com/marchv/UIScreenExtension.git", from: "0.0.11")

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

[email protected]

:)

GitHub

link
Stars: 70
Last commit: 28 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

0.0.11 Support Apple Devices up to September 2023
28 weeks ago

Adds support for the following Apple Devices:

  • iPhone14,6 (iPhone SE 3rd generation)
  • iPhone14,7 (iPhone 14)
  • iPhone15,2 (iPhone 14 Pro)
  • iPhone14,8 (iPhone 14 Plus)
  • iPhone15,3 (iPhone 14 Pro Max)
  • iPhone15,4 (iPhone 15)
  • iPhone16,1 (iPhone 15 Pro)
  • iPhone15,5 (iPhone 15 Plus)
  • iPhone16,2 (iPhone 15 Pro Max)
  • iPad14,1 and iPad14,2 (iPad Mini 6)
  • iPad12,1 and iPad12,2 (iPad 9th generation)
  • iPad13,18 and iPad13,19 (iPad 10th generation. note that this has a larger screen at 10.9" over the 9th generation at 10.2")
  • iPad13,16 and iPad13,17 (iPad Air 5th generation)
  • iPad14,3 and iPad14,4 (iPad Pro 11" 4th generation)
  • iPad14,5 and iPad14,6 (iPad Pro 12.9" 6th generation)

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