PadScaler

PadScaler 是一个用于在 iPhone 和 iPad(11 英寸、13 英寸及未来型号)之间轻松进行 UI 缩放的库。 它可以自动调整界面元素大小以适应不同的屏幕尺寸。

🚀 安装

PadScaler 可以使用 Swift Package Manager (SPM) 安装。

1. 通过 Swift Package Manager 安装

Xcode(推荐方法)

  1. 在 Xcode 中打开 File → Add Packages...
  2. 输入仓库 URL
    https://github.com/quasaryy/PadScaler.git
    
  3. 选择版本 (Up to Next Major Version1.0.0)。
  4. 点击 Add Package 并将 PadScaler 附加到您的 target。

Package.swift 中使用 SPM

如果您手动使用 Swift Package Manager,请在 Package.swift 中添加依赖项

dependencies: [
    .package(url: "https://github.com/quasaryy/PadScaler.git", from: "1.0.0")
]

然后将其导入到您的代码中

import PadScaler

📌 用法

PadScaler 提供了便捷的方法来调整 UI 元素的大小。

1. 缩放尺寸 (scaledSize)

根据标准乘数缩放 iPad 的数值

let scaledValue = 10.scaledSize

结果:

如果您需要设置自定义乘数

let customScaled = 10.scaledSize(smallPadMultiplier: 1.5, largePadMultiplier: 2.3)

如果仅设置了大型 iPad 乘数,则会自动计算小型 iPad 乘数

let autoScaled = 10.scaledSize(largePadMultiplier: 2.3)

结果:

2. 自适应尺寸 (adaptiveSize)

允许为 iPhone、iPad 11" 和 iPad 13" 指定不同的值

let adaptive = adaptiveSize(phone: 5, smallPad: 7.6, largePad: 8.7)

结果:

3. 与 SwiftUI 一起使用

PadScaler 可以很好地与 SwiftUI 配合使用

Text("Adaptive Text")
    .font(.system(size: 16.scaledSize))
    .padding(20.scaledSize)

🛠 配置

1. 全局配置 (shared 实例)

PadScaler.setSharedConfiguration(ScalingConfiguration(
    defaultSmallPadMultiplier: 1.5,
    defaultLargePadMultiplier: 2.2
))

现在 scaledSize 将默认使用这些乘数。

2. 使用自定义实例

如果您需要在不同的地方使用不同的配置,请创建一个单独的实例

let customPadScaler = PadScaler.newInstance(ScalingConfiguration(
    defaultSmallPadMultiplier: 1.8,
    defaultLargePadMultiplier: 2.5
))
let scaled = customPadScaler.scaledSize(10)

📄 许可证

PadScalerMIT 许可证下发布。

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.

🚀 给项目点个星星支持一下!