I'm trying to draw some UIBezierPath objects in a UIView.
These paths are expressed in millimetres and must be drawn with the correct size.
I'm using the method draw(rect:CGRect) of UIView. In my understanding, drawing functions in a UIView are made in points. So I just need to convert the millimetres in points. But if I'm doing that the paths do not have the correct size. On my 12 pro max, the drawing is always is divided by two.
Here my code, the width of the view is equal to width of the screen
I get the following result in the console:
Rect in pt 428.0 845.0
Rect in mm 150.98888888888888 298.09722222222223
We can notice that the width of the rect is twice the screen width of 12 pro max
These paths are expressed in millimetres and must be drawn with the correct size.
I'm using the method draw(rect:CGRect) of UIView. In my understanding, drawing functions in a UIView are made in points. So I just need to convert the millimetres in points. But if I'm doing that the paths do not have the correct size. On my 12 pro max, the drawing is always is divided by two.
Here my code, the width of the view is equal to width of the screen
Code Block // 72 points = 25.4 mm // So 1 mm = 72/25.4 pt func mmToPoints(_ value:CGFloat) -> CGFloat{ return 72.0 * value/25.4 } func pointsToMM(_ value:CGFloat) -> CGFloat{ return value * 25.4/72.0 } class MyView : UIView{ override func draw(_ rect: CGRect) { let width = rect.width let height = rect.height print("Rect in pt \(width) \(height)") print("Rect in mm \(pointsToMM(width)) \(pointsToMM(height))") let path = UIBezierPath() let sizeInMM = CGFloat(50.0); // 50 mm, 5 cm let sizeInPts = mmToPoints(sizeInMM) UIColor.black.setStroke() path.move(to: CGPoint.zero) path.addLine(to: CGPoint(x: sizeInPts, y: 0.0)) path.addLine(to: CGPoint(x: sizeInPts, y: sizeInPts)) path.addLine(to: CGPoint(x: 0.0, y: sizeInPts)) path.stroke() } }
I get the following result in the console:
Rect in pt 428.0 845.0
Rect in mm 150.98888888888888 298.09722222222223
We can notice that the width of the rect is twice the screen width of 12 pro max