Changing a points actual size globally?

Viewed 266

Is there a way to globally change the default scale of an iOS app? With "scale" I mean the physical size the points represent if that makes sense. I don't want to apply any transforms or scaling to a single view, I want it to be applied globally or per-screen basis.

I have tried to modify the nativeScale and scale properties of UIView but it doesn't seem to be possible.

Example

Default behaviour

By default, a square with width 100 vs width 200 is rendered as follows:

Goal

When specifying width 100, I want the square to be rendered with twice the size (points):

Is there a way to achieve this?

2 Answers

"By default, a square with width 100 vs width 200 is rendered as follows:"

I don't think it will work on as you give input but it will work on multiplier of your screen bounds. i am not able to make comment so i share as answer.

take a look here where i share a simple project to understand how it will work.

https://github.com/jatinfl15/ResizeScreenWise

enter image description here

here i assign innerview aspect to width of screen. innerview multiplier assign 0.2.

I do not think it is possible to change CG to use Pixel Coordinates instead of Point Coordinates. There are a number of scaling and rasterization options for CALayer but I do not think they will do what you want.

Here is a programmatic solution that uses an extension so you can do a CGRect() style:

let rectInPixels = CGRect(pointsX: 50.0, y: 50.0, width: 100.0, height: 100.0)    

Results:

rectInPixels: (100.0, 100.0, 200.0, 200.0)

Only difference is you use "pointsX:" instead of "x:" when creating a rect. Of course, you can use whatever argument name that makes most sense to you.

Or convert a rect from points to pixels:

//Create a rect using Point coordinates
let rectInPoints = CGRect(x: 50.0, y: 50.0, width: 100.0, height: 100.0)

//Convert from Points to Pixel coordinates
let rectInPixels = rectInPoints.pixelCoords

Results:

rectInPixels: (100.0, 100.0, 200.0, 200.0)

Of course you can make a similar extension for CGPoint or any other datatype.

Extension:

public extension CGRect {
    init(fromPointCoords pointRect: CGRect) {
        let res = UIScreen.main.nativeScale
        self.init(x: pointRect.origin.x * res, y: pointRect.origin.y * res, width: pointRect.size.width * res, height: pointRect.size.height * res)
    }

    init(pointsX x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) {
        let rectPixels = CGRect(fromPointCoords: CGRect(x, y, width, height))
        self.init(origin: rectPixels.origin, size: rectPixels.size)
    }

    var pixelCoords:CGRect {
        get {
            return CGRect(fromPointCoords: self)
        }
        set(newValue) {}
    }

}
Related