Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func main() {
- let (scale, n) = findMagic()
- let device = UIDevice.current
- let env = ProcessInfo.processInfo.environment
- let model = env["SIMULATOR_MODEL_IDENTIFIER"] ?? {
- var systemInfo = utsname()
- uname(&systemInfo)
- return withUnsafePointer(to: &systemInfo.machine) {
- String(cString: UnsafeRawPointer($0).assumingMemoryBound(to: CChar.self))
- }
- }()
- print(model, device.systemName, device.systemVersion, scale, "->", n)
- }
- func findMagic() -> (CGFloat, Int) {
- let view = UIView()
- view.translatesAutoresizingMaskIntoConstraints = false
- let w = view.widthAnchor.constraint(equalToConstant: 0)
- w.isActive = true
- let scale = UIScreen.main.scale
- var lo: CGFloat = 6.4 / scale
- var hi: CGFloat = 6.6 / scale
- while true {
- let mid = (lo + hi) / 2
- if mid == lo || mid == hi {
- let n = findScale(lo, hi)
- return (scale, n)
- }
- w.constant = mid
- let size = view.systemLayoutSizeFitting(
- UIView.layoutFittingCompressedSize,
- withHorizontalFittingPriority: .fittingSizeLevel,
- verticalFittingPriority: .fittingSizeLevel
- )
- if size.width < 6.5 / scale {
- lo = mid
- } else {
- hi = mid
- }
- }
- }
- func findScale(_ lo: CGFloat, _ hi: CGFloat) -> Int {
- var n = 1
- while round(lo * CGFloat(n)) == round(hi * CGFloat(n)) {
- n += 1
- }
- return n
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement