Skip to content

A library for determining the actual physical size of pixels on an iOS device.

License

Notifications You must be signed in to change notification settings

freesty82/IRLSize

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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<NSUnitLength *> *width = view.irl_physicalWidth;
NSMeasurement<NSUnitLength *> *height = view.irl_physicalHeight;

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

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

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<NSUnitLength *> *desiredHeight =
[[NSMeasurement alloc] initWithDoubleValue:38.0
                                      unit:NSUnitLength.millimeters];

view.transform = [view irl_transformForPhysicalHeight:desiredHeight];

Swift

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

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

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;

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

Swift

// iOS
let screenHeight = UIDevice.current.physicalScreenHeight

// watchOS
let screenHeight = WKInterfaceDevice.current.phsyicalScreenHeight

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.

About

A library for determining the actual physical size of pixels on an iOS device.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 91.4%
  • Swift 5.7%
  • Ruby 2.3%
  • C 0.6%