LoginSignup
6
9

More than 5 years have passed since last update.

iOSで画面上に実寸のViewを表示できるライブラリの紹介

Last updated at Posted at 2017-01-08

iOSで実寸のCGRect, CGSizeなどを作ることが出来るライブラリ、TrueScaleを公開したので紹介します。

GitHub:TrueScale

インストール

Carthage

github "ReoHokazono/TrueScale"

CocoaPods

pod 'TrueScale'

ファイルを直接プロジェクトに追加

TrueScale.swiftをプロジェクトに直接追加してください

使用方法

[mm]からCGRectを作成

let frame = TSRect(x: 10, y: 10, width: 20, height: 20, unit: .mm).cgrect
let square = UIView(frame: frame)
square.backgroundColor = UIColor.darkGray

[cm],[inch]も使用可能です

//[cm]
let frame = TSRect(x: 1, y: 1, width: 2, height: 2, unit: .cm).cgrect

//[inch]
let frame = TSRect(x: 1, y: 1, width: 2, height: 2, unit: .inch).cgrect

CGPoint,CGSizeも同様に作成できます。

let size = TSSize(width: 30, height: 30, unit: .mm).cgsize
let point = TSPoint(x: 1, y: 1, unit: .inch).cgpoint

CGFloat単体で値を取得することもできます。

let pointValue = CGFloat(1).toPoint(unit: .cm)

仕組み

Appleの公式サイトに掲載されている画面のインチと、開発時に使用するポイントの対角線の長さの比率から計算しています。

精度について

誤差は1mm以内に収まっていると思います。端末による差異も考えられるのでシリアスな用途での使用は避けてください。

6
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9