{"id":17463864,"url":"https://github.com/linxunfeng/swiftyfitsize","last_synced_at":"2025-04-06T22:09:14.666Z","repository":{"id":37677659,"uuid":"153405562","full_name":"LinXunFeng/SwiftyFitsize","owner":"LinXunFeng","description":"📱 Swifty screen adaptation solution (Support Objective-C and Swift)","archived":false,"fork":false,"pushed_at":"2023-03-18T04:18:33.000Z","size":2086,"stargazers_count":289,"open_issues_count":0,"forks_count":42,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-30T20:12:55.477Z","etag":null,"topics":["font","ios","number","objc","objcective-c","oc","screen","screenadapter","storyboard","swift","swifty","xib"],"latest_commit_sha":null,"homepage":"https://fullstackaction.com","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LinXunFeng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"ko_fi":"linxunfeng"}},"created_at":"2018-10-17T06:21:46.000Z","updated_at":"2024-12-18T08:22:27.000Z","dependencies_parsed_at":"2022-09-13T19:31:18.809Z","dependency_job_id":"a224e419-a7ea-4fa1-afee-469f5ae80de9","html_url":"https://github.com/LinXunFeng/SwiftyFitsize","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinXunFeng%2FSwiftyFitsize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinXunFeng%2FSwiftyFitsize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinXunFeng%2FSwiftyFitsize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LinXunFeng%2FSwiftyFitsize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LinXunFeng","download_url":"https://codeload.github.com/LinXunFeng/SwiftyFitsize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247557767,"owners_count":20958047,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["font","ios","number","objc","objcective-c","oc","screen","screenadapter","storyboard","swift","swifty","xib"],"created_at":"2024-10-18T10:44:00.713Z","updated_at":"2025-04-06T22:09:14.640Z","avatar_url":"https://github.com/LinXunFeng.png","language":"Swift","funding_links":["https://ko-fi.com/linxunfeng"],"categories":[],"sub_categories":[],"readme":"# SwiftyFitsize\n\n![Author](https://img.shields.io/badge/author-LinXunFeng-blue.svg?style=flat-square)\n[![CI Status](https://img.shields.io/travis/LinXunFeng/SwiftyFitsize.svg?style=flat)](https://travis-ci.org/LinXunFeng/SwiftyFitsize)\n[![Version](https://img.shields.io/cocoapods/v/SwiftyFitsize.svg?style=flat)](https://cocoapods.org/pods/SwiftyFitsize)\n[![License](https://img.shields.io/cocoapods/l/SwiftyFitsize.svg?style=flat)](https://cocoapods.org/pods/SwiftyFitsize)\n[![Platform](https://img.shields.io/cocoapods/p/SwiftyFitsize.svg?style=flat)](https://cocoapods.org/pods/SwiftyFitsize)\n![visitors](https://visitor-badge.glitch.me/badge?page_id=LinXunFeng.SwiftyFitsize)\n\n## Example\n\n克隆或下载仓库到本地后，请先运行 `pod install` , 再打开 `SwiftyFitsize.xcworkspace`\n\n## Requirements\n\n- iOS 9.0+\n- Xcode 10.2+\n- Swift 5+\n\n## Installation\n\n- Cocoapods\n\n```ruby\nuse_frameworks!\npod 'SwiftyFitsize'\n```\n\n\n\n- Swift Package Manager\n\n```\n1. File \u003e Add Packages\n2. Search https://github.com/LinXunFeng/SwiftyFitsize.git\n3. Select \"Up to Next Major\" with \"1.4.1\"\n```\n\n\n\n## Exhibition\n\n![iPhone](https://github.com/LinXunFeng/SwiftyFitsize/raw/master/Screenshots/exhibition.png)\n\n\n\n![iPad](./Screenshots/ipad.png)\n\n\n\n## Usage\n\n### 一、运算符\n\n\u003e 无论是 `~` 还是 `≈` 对 `iPhone` 的适配效果是一样的。而对 `iPad` 而言，`iPad` 的宽度太大，使用 `≈` 还是会按宽度比例进行运算，就会显示特别臃肿，这时使用 `~` 在显示上就会比较合适。\n\u003e\n\u003e - `~` 在  `≈` 的基础上针对 `iPad` 的适配大小后再去乘上 `iPadFitMultiple` 。\n\u003e- 一般情况下直接使用 `~` 即可。\n\u003e - 其它运算符亦是如此，具体请看下方的说明表\n\n\n\n| 运算符 | 说明                                                         |\n| ------ | ------------------------------------------------------------ |\n| `~`    | 对比宽度，当设备为 `iPad` 时，适配后的 `value` 会再乘上 `iPadFitMultiple` |\n| `≈`    | 对比宽度，强制适配，不论是 `iPhone` 还是 `iPad` 都不会乘上 `iPadFitMultiple` |\n| `∣`    | 对比高度，对应 `~` ，整屏高度                                |\n| `∥`    | 对比高度，对应 `≈` ，整屏高度                                |\n| `∣=`   | 对比高度，对应 `∣` ，安全区域内的高度                        |\n| `∥=`   | 对比高度，对应 `∥` ，安全区域内的高度                        |\n| `∣-`   | 对比高度，对应 `∣` ，除去刘海区域的安全区域内的高度          |\n| `∥-`   | 对比高度，对应 `∥` ，除去刘海区域的安全区域内的高度          |\n\n各高度适配的对比\n\n\u003e- 红色:  `∣` 和 `∥` ，以整个屏幕的高度进行适配\n\u003e- 蓝色:  `∣=` 和 `∥=`  以中心安全区域的高度进行适配\n\u003e- 绿色:  `∣-` 和 `∥-`  以包括底部的安全区域进行适配\n\n![高度适配对比](./Screenshots/fitsize-height.jpg)\n\n\n\n举个例子\n\n`~` : 当设备为 `iPad` 时，适配后的值会与 `iPadFitMultiple` 相乘\n\n```swift\n100~\nUIFont.systemFont(ofSize: 14)~\nCGPoint(x: 10, y: 10)~\nCGRect(x: 10, y: 10, width: 100, height: 100)~\nUIEdgeInsetsMake(10, 10, 10, 10)~\n```\n\n\n\n`≈` :  (option + x)  适配后的值不会与 `iPadFitMultiple` 相乘\n\n```swift\n100≈\nUIFont.systemFont(ofSize: 14)≈\nCGPoint(x: 10, y: 10)≈\nCGRect(x: 10, y: 10, width: 100, height: 100)≈\nUIEdgeInsetsMake(10, 10, 10, 10)≈\n```\n\n\n\n修改 `参照宽度` 、`参照高度`、`是否为iPhoneX系列的参照高度`与 `iPadFitMultiple`  可以调用如下方法\n\n```swift\n/// 设置参照的相关参数\n///\n/// - Parameters:\n///   - width: 参照的宽度\n///   - height: 参照的高度\n///   - isIPhoneXSeriesHeight: 是否为iPhoneX系列的参照高度\n///   - iPadFitMultiple: iPad 在适配后所得值的倍数 (0 , 1]\nSwiftyFitsize.reference(\n  width: 414, \n  height: 896, \n  isIPhoneXSeriesHeight: true, \n  iPadFitMultiple: 0.5\n)\n```\n\n\n\n### 二、支持 xib 和 storyboard\n\n```swift\nenum SwiftyFitType: Int {\n    /// Original Value\n    case none = 0\n    /// ~\n    case flexibleWidth = 1\n    /// ≈\n    case forceWidth = 2\n    /// ∣\n    case flexibleHeight = 3\n    /// ∥\n    case forceHeight = 4\n    /// ∣=\n    case flexibleSafeAreaCenterHeight = 5\n    /// ∥=\n    case forceSafeAreaCenterHeight = 6\n    /// ∣-\n    case flexibleSafeAreaWithoutTopHeight = 7\n    /// ∥-\n    case forceSafeAreaWithoutTopHeight = 8\n}\n```\n\n- Font Fitsize\n\n支持的UI控件有： `UILabel` `UIButton` `UITextView` `UITextField`\n\n`FontFitType` 的值请参考上方的 `enum SwiftyFitType`\n\n![xib-font](./Screenshots/xib-font.png)\n\n\n\n- Constraint Fitsize\n\n约束适配同上\n\n![xib-font](./Screenshots/xib-constraint.png)\n\n\n\n`iPad` 关于 `~` 与 `≈`  在使用上的对比\n\n![~与≈的对比](./Screenshots/flexible-force.jpg)\n\n\n\n\n\n### 三、Objective-C\n\n\u003e 1. 由于 `OC` 不支持运算符重载，所以只能用宏来适配。\n\u003e\n\u003e 2. `Xib` 和  `Storyboard`  则跟上方提及的使用方式相同。\n\n\n\n`Swift运算符` 与 `OC宏` 对应表\n\n| Swift运算符 | OC宏         |\n| ----------- | ------------ |\n| `~`         | `SF_xx`      |\n| `≈`         | `SFZ_xx`     |\n| `∣`         | `SF_FH_xx`   |\n| `∥`         | `SFZ_FH_xx`  |\n| `∣=`        | `SF_SCH_xx`  |\n| `∥=`        | `SFZ_SCH_xx` |\n| `∣-`        | `SF_SBH_xx`  |\n| `∥-`        | `SFZ_SBH_xx` |\n\n注: `xx` 支持如下类型 `Font` 、`Int`、`Float`、`Point`、`Size`、`Rect`、`EdgeInsets`\n\n\n\n- 导入\n\n```objc\n@import SwiftyFitsize;\n```\n\n\u003e 注：如果你使用的是 `SPM` 添加的依赖，且需要使用到宏（如：`SF_Float`），\n\u003e\n\u003e 则需要导入 `SwiftyFitsizeOCSupport` ，而 `SwiftyFitsize` 可以不导入。\n\n```objc\n@import SwiftyFitsizeOCSupport;\n```\n\n\n\n- 修改 `参照宽度` 、`参照高度`、`是否为iPhoneX系列的参照高度`与 `iPadFitMultiple`  \n\n```objc\n[SwiftyFitsize referenceWithWidth:414\n                           height:896\n            isIPhoneXSeriesHeight:YES\n                  iPadFitMultiple:0.6];\n```\n\n- `~`\n\n```objc\nUIFont *font = [UIFont systemFontOfSize:14];\n\nUIFont *font1 = font.sf;\nUIFont *font2 = SF_Font(font);\n\nCGFloat num = SF_Float(14);\nCGPoint point = SF_Point(CGPointMake(10, 10));\nCGSize size = SF_Size(CGSizeMake(100, 100));\nCGRect rect = SF_Rect(CGRectMake(10, 10, 100, 100));\nUIEdgeInsets edge = SF_EdgeInsets(UIEdgeInsetsMake(0, 0, 100, 100));\n\n```\n\n- `≈`\n\n```objc\nUIFont *font1 = font.sfz;\nUIFont *font2 = SFZ_Font(font);\n\nCGFloat num = SFZ_Float(14);\nCGPoint point = SFZ_Point(CGPointMake(10, 10));\nCGSize size = SFZ_Size(CGSizeMake(100, 100));\nCGRect rect = SFZ_Rect(CGRectMake(10, 10, 100, 100));\nUIEdgeInsets edge = SFZ_EdgeInsets(UIEdgeInsetsMake(0, 0, 100, 100));\n```\n\n其它运算符亦是如此使用\n\n\n\n### 四、移除指定尺寸后适配\n\n\u003e 场景：`tableView` 的左右间距在屏幕大小不同的情况下都为 `10`，在排除左右为 `10` 的间距后，再对`Cell` 进行等比适配\n\u003e\n\u003e 使用思路：宽度一共减去 `20` ，以剩下的大小来做适配\n\n#### Swift\n\n建议使用 `PropertyWrapper` 方案，供下面的调用方式一和二使用\n\n```swift\n\nstruct Metric {\n    static let tableViewLeftRightMargin: CGFloat = 10 // 定义 tableView 的左右间距\n    ...\n    static let tableViewHeight: CGFloat = Fit.$width(30) // 去掉左右边距后进行适配的值\n    static let rowLeftViewWidth: CGFloat = Fit.$width(177.5)\n    static let rowCenterViewWidth: CGFloat = Fit.$width(100.5)\n    static let rowRightViewWidth: CGFloat = Fit.$width(77)\n    ...\n}\n\nstruct Fit {\n//    @WrappedSwiftyFitsize(fitType: .flexibleWidth, reduceValue: Metric.tableViewLeftRightMargin)\n//    fitType 默认值是 .flexibleWidth，所以可以不传\n    @WrappedSwiftyFitsize(reduceValue: Metric.tableViewLeftRightMargin * 2)\n    static var width: CGFloat = 375\n}\n```\n\n`375` 是初始化值，没有特殊意义，只为调用 `Fit.width` 时可以取到值，如果用不到 `Fit.width` 的值可以不进行初始化\n\n调用方式：\n\n```swift\n// 移除指定尺寸后的适配，调用方式：\n// 以下都是以适配 tableView 为例，移除 tableView 左右两侧固定的边距，以剩余的宽度来做适配\n\n// 方式一：先赋值再取值\n// 将 20 进行适配\nFit.width = 20\nprint(\"适配后的值 -- \\(Fit.width)\")\n\n// 方式二：使用 $ 将 width 当方法用，传入待适配的值\n// 将 30 进行适配\nlet aVal = Fit.$width(30)\nprint(\"适配后的值 aVal -- \\(aVal)\")\n\n// 方式三：调用 SwiftyFitsize.fit 方法\nlet bVal = SwiftyFitsize.fit(\n  size: 40,\n  fitType: .flexibleWidth,\n  reduceValue: Metric.tableViewLeftRightMargin * 2\n)\nprint(\"适配后的值 bVal -- \\(bVal)\")\n```\n\n\n\n#### Objective-C\n\n这是还是以适配 `tableView` 为例，移除 tableView 左右两侧固定的边距，以剩余的宽度来做适配\n\n```objc\nCGFloat fitWidth = [SwiftyFitsize fitWithSize:40\n                                      fitType:SwiftyFitTypeFlexibleWidth\n                                  reduceValue:20];\n```\n\n可以将其做为宏，以方便使用\n\n```objc\n#define kFitWidth(value) \\\n[SwiftyFitsize fitWithSize:value fitType:SwiftyFitTypeFlexibleWidth reduceValue:20]\n```\n\n使用宏来做适配\n\n```objc\nCGFloat fitWidth = kFitWidth(40);\nNSLog(@\"fitWidth -- %f\", fitWidth);\n```\n\n\n\n效果如包含红绿蓝的条视图所示：\n\n![高度适配对比](./Screenshots/fitsize-reduce.jpg)\n\n\n\n### 五、计算结果类型\n\n#### 定义\n\n```swift\n/// 计算结果类型\n@objc public enum SwiftyFitCalcResultType: Int {\n    /// 跟随全局配置\n    case globalConfig\n    /// 原始数据\n    case raw\n    /// 四舍五入\n    case round\n    /// 保留一位小数（根据第二位小数进行四舍五入）\n    case oneDecimalPlace\n}\n```\n\n#### 影响范围\n\n- `.raw ` : 影响忽略不计\n- `.round` : 影响范围 `(-0.5, 0.5]`\n- `.oneDecimalPlace` : 影响范围 `(-0.05, 0.05]`\n\n#### 全局配置\n\n\u003e - 不配置则默认为 `.raw`\n\u003e - `.globalConfig` 取的就是这里配置的类型\n\u003e - 如果在该处还是设置为 `.globalConfig`，则内部会将其重置为 `.raw`\n\n```swift\nSwiftyFitsize.reference(width: 375, calcResultType: .oneDecimalPlace) // 全局配置计算结果为保留一位小数\n```\n\n#### 使用\n\n\u003e  以下在不指定 `calcResultType` 参数的情况下，默认都是跟随全局配置\n\n单独指定 `calcResultType`\n\n```swift\nSwiftyFitsize.fit(\n    size: 35, // 36\n    fitType: .flexibleWidth,\n    reduceValue: 10 * 2,\n    calcResultType: .raw // .round .oneDecimalPlace\n)\n```\n\n`PropertyWrapper` 方式\n\n```swift\n// calcResultType: .raw .round .oneDecimalPlace\n@WrappedSwiftyFitsize(reduceValue: Metric.tableViewLeftRightMargin * 2, calcResultType: .raw)\nstatic var width: CGFloat = 375\n```\n\n#### 示例数据\n\n- 第一列为适配前的数值\n\n- 其它列为适配后根据不同的 `SwiftyFitCalcResultType` 计算得到的值\n\n| 原值 | raw                | round | oneDecimalPlace |\n| ---- | ------------------ | ----- | --------------- |\n| 35   | 36.478873239436616 | 36.0  | 36.5            |\n| 36   | 37.52112676056338  | 38.0  | 37.5            |\n\n\n\n## Misc\n\n下面列出一些设备对应的分辨率，方便查找\n\n| 设备                    | 逻辑分辨率(point) | 设备分辨率(pixel) |\n| ----------------------- | ----------------- | ----------------- |\n| SE                      | 320x568           | 640x1136          |\n| 6(S)／7／8 / SE(第二代) | 375x667           | 750x1334          |\n| 6(S)+／7+／8+           | 414x736           | 1080x1920         |\n| X(S) / 11Pro            | 375x812           | 1125x2436         |\n| XR / 11                 | 414x896           | 828x1792          |\n| XS Max / 11Pro Max      | 414x896           | 1242x2688         |\n| 12 mini / 13 mini       | 360x780           | 1080x2340         |\n| 12(Pro) / 13(Pro)       | 390x844           | 1170x2532         |\n| 12 Pro Max / 13 Pro Max | 428x926           | 1284x2778         |\n\n\n\n## License\n\nSwiftyFitsize is available under the MIT license. See the LICENSE file for more info.\n\n\n\n## Author\n\n- LinXunFeng\n- email: [linxunfeng@yeah.net](mailto:linxunfeng@yeah.net)\n- Blogs: [全栈行动](https://fullstackaction.com/) | [LinXunFeng‘s Blog](http://linxunfeng.top/)  | [掘金](https://juejin.im/user/58f8065e61ff4b006646c72d/posts) \n\n\n\u003cimg height=\"267.5\" width=\"481.5\" src=\"https://github.com/LinXunFeng/LinXunFeng/blob/master/static/img/FSAQR.png\" /\u003e\n\n\n## Other\n\n| \u003cimg width=\"252\" height=\"294\" src=\"https://github.com/LinXunFeng/site/raw/master/source/images/others/pay/alipay_tip.png\" /\u003e | \u003cimg width=\"252\" height=\"294\"  src=\"https://github.com/LinXunFeng/site/raw/master/source/images/others/pay/wechat_tip.png\" /\u003e |\n| :----------------------------------------------------------: | :----------------------------------------------------------: |\n|                                                              |                                                              |\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinxunfeng%2Fswiftyfitsize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinxunfeng%2Fswiftyfitsize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinxunfeng%2Fswiftyfitsize/lists"}