PadScaler 是一个用于在 iPhone 和 iPad(11 英寸、13 英寸及未来型号)之间轻松进行 UI 缩放的库。 它可以自动调整界面元素大小以适应不同的屏幕尺寸。
PadScaler 可以使用 Swift Package Manager (SPM) 安装。
https://github.com/quasaryy/PadScaler.git
Up to Next Major Version
→ 1.0.0
)。Add Package
并将 PadScaler
附加到您的 target。如果您手动使用 Swift Package Manager,请在 Package.swift
中添加依赖项
dependencies: [
.package(url: "https://github.com/quasaryy/PadScaler.git", from: "1.0.0")
]
然后将其导入到您的代码中
import PadScaler
PadScaler 提供了便捷的方法来调整 UI 元素的大小。
根据标准乘数缩放 iPad 的数值
let scaledValue = 10.scaledSize
✅ 结果:
10
16
20
如果您需要设置自定义乘数
let customScaled = 10.scaledSize(smallPadMultiplier: 1.5, largePadMultiplier: 2.3)
如果仅设置了大型 iPad 乘数,则会自动计算小型 iPad 乘数
let autoScaled = 10.scaledSize(largePadMultiplier: 2.3)
✅ 结果:
10
1.839
(自动计算)23
允许为 iPhone、iPad 11" 和 iPad 13" 指定不同的值
let adaptive = adaptiveSize(phone: 5, smallPad: 7.6, largePad: 8.7)
✅ 结果:
5
7.6
8.7
PadScaler 可以很好地与 SwiftUI 配合使用
Text("Adaptive Text")
.font(.system(size: 16.scaledSize))
.padding(20.scaledSize)
PadScaler.setSharedConfiguration(ScalingConfiguration(
defaultSmallPadMultiplier: 1.5,
defaultLargePadMultiplier: 2.2
))
现在 scaledSize
将默认使用这些乘数。
如果您需要在不同的地方使用不同的配置,请创建一个单独的实例
let customPadScaler = PadScaler.newInstance(ScalingConfiguration(
defaultSmallPadMultiplier: 1.8,
defaultLargePadMultiplier: 2.5
))
let scaled = customPadScaler.scaledSize(10)
PadScaler 在 MIT 许可证下发布。
Copyright (c) 2025 Yury Lebedev
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
🚀 给项目点个星星支持一下! ⭐