Skip to content
A library for determining the actual physical size of pixels on an iOS device.
Objective-C Ruby Swift C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Example Update dependencies. Oct 7, 2019
IRLSizeTests Add support for iPad (7th Generation) Sep 24, 2019
Pod/Classes Add support for iPad (7th Generation) Sep 24, 2019
.gitignore Update code signing ignore setup Nov 6, 2018
.ruby-version
.travis.yml Use Xcode 11 on Travis Sep 17, 2019
CHANGELOG.md Add support for iPad (7th Generation) Sep 24, 2019
Gemfile Update the remaining metadata. Apr 29, 2019
Gemfile.lock Update dependencies. Oct 7, 2019
IRLSize.podspec Add support for iPad (7th Generation) Sep 24, 2019
LICENSE Update to Orchard from SDVersion. Mar 23, 2019
README.md Update documentation. Nov 6, 2018

README.md

IRLSize

CI Status Version License Platform

Getting Started

To run the example project, clone the repo and navigate to its root folder in Terminal. Run the following command to install dependencies in their proper location:

pod install --project-directory=Example

Use

All IRLSize methods have two versions: one that uses NSMeasurement (Measurement in Swift), and one that uses the IRLRawMillimeters type (a double measuring millimeters).

Important: Upgrading to IRLSize 2.0

IRLSize 2.0 switched the native unit for measurements from meters to millimeters to better manage what official Apple documentation there is on device sizes. This is a breaking change and as such the raw unit type changed from IRLRawLengthMeasurement to IRLRawMillimeters to avoid this change accidentally changing existing code by a factor of 1,000. If you were using NSMeasurement, no updates should be necessary, as it automatically converts between units.

Measuring an On-Screen Element

To find out the dimensions of a UIView in an iOS app, use one of the following properties:

Objective-C

// NSMeasurement Version
NSMeasurement<NSUnitLength *> *width = view.irl_physicalWidth;
NSMeasurement<NSUnitLength *> *height = view.irl_physicalHeight;

// Raw Version
//   `IRLRawMillimeters` is a `double` representing millimeters. 
IRLRawMillimeters rawWidth = view.irl_rawPhysicalWidth;
IRLRawMillimeters rawHeight = view.irl_rawPhysicalHeight;

If a view is on a secondary screen (i.e. if you’re using an external display) the measurements will be returned as nil.

Of course, this also works nicely in Swift:

Swift

// Measurement Version
let width = view.physicalWidth // type: Measurement<UnitLength>
let height = view.physicalHeight // type: Measurement<UnitLength>

// Raw Version
let rawWidth = view.rawPhysicalWidth // type: IRLRawMillimeters
let rawHeight = view.rawPhysicalHeight // type: IRLRawMillimeters

Sizing a View

If you want to ensure that a view matches a certain physical size, IRLSize provides transforms to help you out:

Objective-C

// NSMeasurement Version
NSMeasurement<NSUnitLength *> *desiredHeight =
[[NSMeasurement alloc] initWithDoubleValue:38.0
                                      unit:NSUnitLength.millimeters];

view.transform = [view irl_transformForPhysicalHeight:desiredHeight];

// Raw Version
view.transform = [view irl_transformForRawPhysicalHeight:38.0];

Swift

// Measurement Version
let desiredHeight = Measurement(value: 38, unit: UnitLength.millimeters)

view.transform = view.transform(forPhysicalHeight:desiredHeight)

// Raw Version
view.transform = view.transform(forRawPhysicalHeight:38);

Measuring a Device

If you just want to know the physical size of the screen, use the category on UIDevice for iOS or WKInterfaceDevice for watchOS:

Objective-C

// iOS
NSMeasurement<NSUnitLength *> *screenHeight = UIDevice.currentDevice.irl_physicalScreenHeight;
IRLRawMillimeters rawScreenHeight = UIDevice.currentDevice.irl_rawPhysicalScreenHeight;

// watchOS
NSMeasurement<NSUnitLength *> *screenHeight = WKInterfaceDevice.currentDevice.irl_physicalScreenHeight;
IRLRawMillimeters rawScreenHeight = WKInterfaceDevice.currentDevice.irl_rawPhysicalScreenHeight;

Swift

// iOS
let screenHeight = UIDevice.current.physicalScreenHeight
let rawScreenHeight = UIDevice.current.rawPhysicalScreenHeight

// watchOS
let screenHeight = WKInterfaceDevice.current.phsyicalScreenHeight
let rawScreenHeight = WKInterfaceDevice.current.rawPhysicalScreenHeight

Installation

IRLSize is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "IRLSize"

Author

Jeff Kelley (SlaunchaMan@gmail.com) at Detroit Labs.

License

IRLSize is available under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.