DynamicSize

PHOTO EMBED

Wed Jul 14 2021 16:45:27 GMT+0000 (Coordinated Universal Time)

Saved by @Ibrahim #swift

//  Converted by Storyboard to SwiftUI Converter - https://swiftify.com/#/converter/storyboard2swiftui

import SwiftUI

// --------------------------------------------------------------------------------
// UIViewController
// --------------------------------------------------------------------------------
struct View1: View {
    var body: some View {
        ZStack(alignment: .topLeading) {
            GeometryReader { geometry in
                Text("Hello SwiftUI!")
                    .frame(dynamicWidth: 126.5, dynamicHeight: 28.5)
                    .font(.custom("ChalkboardSE-Regular", size: 20))
                    .offset(dyanmicX: 124.5, dynamicY: 319.5)
            }
        }
        .frame(dynamicWidth: 375, dynamicHeight: 667)
        .background(Color(.systemBackground))
        .edgesIgnoringSafeArea(.all)
    }
}

// --------------------------------------------------------------------------------
// Dynamic Size Helper
// --------------------------------------------------------------------------------
struct DynamicSize {
    static private let baseViewWidth: CGFloat = 320.0
    static private let baseViewHeight: CGFloat = 568.0

    static func getHeight(_ height: CGFloat) -> CGFloat {
        return (height / baseViewHeight) * UIScreen.main.bounds.height
    }

    static func getWidth(_ width: CGFloat) -> CGFloat {
        return (width / baseViewWidth) * UIScreen.main.bounds.width
    }

    static func getOffsetX(_ x: CGFloat) -> CGFloat {
        return (x / baseViewWidth) * UIScreen.main.bounds.width
    }

    static func getOffsetY(_ y: CGFloat) -> CGFloat {
        return (y / baseViewHeight) * UIScreen.main.bounds.height
    }
}

struct View1_Previews: PreviewProvider {
    static var previews: some View {
        View1()
    }
}


// --------------------------------------------------------------------------------
// Frame and Offset Helper
// --------------------------------------------------------------------------------
extension View {
    func frame(dynamicWidth: CGFloat? = nil, dynamicHeight: CGFloat? = nil) -> some View {
        self.frame(width: DynamicSize.getWidth(dynamicWidth ?? 0), height: DynamicSize.getHeight(dynamicHeight ?? 0))
    }
    
    func offset(dyanmicX: CGFloat = 0, dynamicY: CGFloat = 0) -> some View {
        self.offset(x: DynamicSize.getOffsetX(dyanmicX), y: DynamicSize.getOffsetY(dynamicY))
    }
}
content_copyCOPY