Last active
July 27, 2022 08:06
-
-
Save tixster/75d0e49826f7701ee405562030124389 to your computer and use it in GitHub Desktop.
Size Customizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // CGFloat.swift | |
| // | |
| // | |
| // Created by kirill.tila on 14.06.2022. | |
| // | |
| // Рашсирение для удобной установки размеров | |
| import UIKit // SwiftUI | |
| extension UIScreen { | |
| static var isSE: Bool { | |
| return main.bounds.width < 375 | |
| } | |
| static var isDefault: Bool { | |
| return main.bounds.height < 812 && main.bounds.width >= 375 | |
| } | |
| static var isX: Bool { | |
| return main.bounds.height >= 812 && main.bounds.height < 1024 | |
| } | |
| static var isPad: Bool { | |
| return main.bounds.height >= 1024 | |
| } | |
| } | |
| public enum DeviceSizeType { | |
| case `default` | |
| case x | |
| case se | |
| case pad | |
| } | |
| public enum SizeClass { | |
| case height | |
| case width | |
| case custom(size: CGFloat) | |
| var size: CGFloat { | |
| switch self { | |
| case .height: return UIScreen.main.bounds.height | |
| case .width: return UIScreen.main.bounds.width | |
| case .custom(let size): return size | |
| } | |
| } | |
| } | |
| public extension CGFloat { | |
| static var screenHeight: Self { | |
| return UIScreen.main.bounds.height | |
| } | |
| static var screenWidth: Self { | |
| return UIScreen.main.bounds.width | |
| } | |
| static func setSize(for sizeClass: SizeClass, size value: Self, defaultSizeClass: Self) -> CGFloat { | |
| let factor: Self = value / defaultSizeClass | |
| return sizeClass.size * factor | |
| } | |
| func setFor(_ type: DeviceSizeType..., size: Self...) -> Self { | |
| var completeSize: Self = self | |
| type.enumerated().forEach { index, currentType in | |
| let indexSize = (size.count - 1) >= index ? size[index] : size[size.count - 1] | |
| let newSize = size.count == 1 ? size[0] : indexSize | |
| if currentType == .se && UIScreen.isSE { | |
| completeSize = newSize | |
| } | |
| if currentType == .x && UIScreen.isX { | |
| completeSize = newSize | |
| } | |
| if currentType == .default && UIScreen.isDefault { | |
| completeSize = newSize | |
| } | |
| if currentType == .pad && UIScreen.isPad { | |
| completeSize = newSize | |
| } | |
| } | |
| return completeSize | |
| } | |
| } | |
| public extension Int { | |
| func setFor(_ type: DeviceSizeType..., size: CGFloat...) -> CGFloat { | |
| var completeSize: CGFloat = CGFloat(self) | |
| type.enumerated().forEach { index, currentType in | |
| let indexSize = (size.count - 1) >= index ? size[index] : size[size.count - 1] | |
| let newSize = size.count == 1 ? size[0] : indexSize | |
| if currentType == .se && UIScreen.isSE { | |
| completeSize = newSize | |
| } | |
| if currentType == .x && UIScreen.isX { | |
| completeSize = newSize | |
| } | |
| if currentType == .default && UIScreen.isDefault { | |
| completeSize = newSize | |
| } | |
| if currentType == .pad && UIScreen.isPad { | |
| completeSize = newSize | |
| } | |
| } | |
| return CGFloat(completeSize) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment