Code Monkey home page Code Monkey logo

irlsize's Introduction

IRLSize

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 ([email protected]) at Detroit Labs.

License

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

irlsize's People

Contributors

dobster avatar nwdl avatar rist avatar slaunchaman avatar taboulot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

irlsize's Issues

help

I've a measure in inch and I want convert it to a frame size; how do I do it ? it's not obvious from the documentation; there is no UnitLength that can be associated to the current device "point" size

let desiredWidth = Measurement(value: 10, unit: UnitLength.inches)
let desiredHeight = Measurement(value: 10, unit: UnitLength.inches)

let size = CGSize(width: ??, height: ??)

thanks in advance

iOS 8 compatibility

Hi!

Thanks for the great work!
I have a q: Is there a certain reason you upped the deployment target to 10.0? I used the lib previously (I think 0.4.3) and it was compatible with 8.0. Most apps need at least compatibility with 9.0 nowadays..

New device support question

Hi @SlaunchaMan πŸ‘‹πŸΌ!

Context

I'm currently developing an application for medical purpose. I have some screens on which I have to display elements in millimeters regardless the device.

First thanks for your library it really helped me 😁

But I didn't anticipate that your library is based on constant that you have to update every time a new apple device is released.

What I understand is that for doing the conversion from millimeters to points, we need the physical size of the device.

My Problem

I can't guaranteed to my client that our application will work with future apple devices.
Our release process is very long. For each release, we have to follow a process that can take several days/weeks...
So creating a new release for every new iPhone is a process that we would like to avoid.

Question

I see that providing the real physical size of a device is not provided in iOS SDK. This is why you have to maintain a list of constant.

Is there a way to do the conversion from millimeters to points without maintaining constants for each device ?
Or are we condemned to deal with this limitation ?

Thanks for your answers and help πŸ˜„

SDVersion is out of date

Would it be possible to add the content of SDVersion to your library as the dependency to that pod is useless unless it's maintained.

It has not been updated for the latest iPhones.

How to resize a UIImage in mm?

Is it possible to apply this framework to a UIImage? I want to resize an image to 152 mm x 203 mm for printing on paper?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.