定义CGSize、CGRect、CGFloat...这些结构体报错解决方法

本文介绍了一种常见的代码编译时出现的错误及其解决方法。当遇到满屏红色标记的编译错误时,在.h文件中导入UIKit/UIKit.h头文件即可解决问题。

如果当你写代码的时候,遇到这种,满屏的红sai惊恐

像这样:


解决方法就是:

在.h文件中导入头文件:#import <UIKit/UIKit.h>就

✅ **完全可以!而且非常推荐** 将这两个方法放在一起。 你现在的这个新方法: ```swift func getMultiLinesLabelViewBound(label: UILabel, width: CGFloat = .greatestFiniteMagnitude, height: CGFloat = .greatestFiniteMagnitude) -> CGRect ``` 它的职责是:**计算多行文本实际渲染所需的边界矩形(CGRect)**,包括宽度和高度的自动测量能力,比只返回高度更完整。 而我们刚刚在 `LayoutCalculator.swift` 中定义的是: - `textHeight(...)` → 只算高度 - `labelHeight(...)` → 基于 label 算高度 所以你现在提出的这个方法是一个**功能更强、更通用的版本**,理应作为核心工具被整合进去。 --- ### ✅ 推荐做法:将它也迁移到 `LayoutCalculator.swift` 中,并升级为静态方法 --- ### ✅ 更新后的 `LayoutCalculator.swift` 内容如下: ```swift // LayoutCalculator.swift import UIKit struct LayoutCalculator { /// 计算给定文本、字体、约束尺寸下的文本所需布局空间(返回CGRect) static func textBounds( text: String?, font: UIFont, constrainedSize: CGSize = CGSize(width: .greatestFiniteMagnitude, height: .greatestFiniteMagnitude) ) -> CGRect { guard let text = text, !text.isEmpty else { return .zero } let attributes: [NSAttributedString.Key: Any] = [ .font: font ] return (text as NSString).boundingRect( with: constrainedSize, options: .usesLineFragmentOrigin, attributes: attributes, context: nil ) } /// 基于 UILabel 实例,计算其文本在指定最大宽高下的实际占用区域 static func labelBounds( label: UILabel, maxWidth: CGFloat = .greatestFiniteMagnitude, maxHeight: CGFloat = .greatestFiniteMagnitude ) -> CGRect { let constrainedSize = CGSize(width: maxWidth, height: maxHeight) return textBounds(text: label.text, font: label.font, constrainedSize: constrainedSize) } /// 兼容旧需求:仅获取高度 static func textHeight(text: String?, font: UIFont, width: CGFloat) -> CGFloat { return textBounds(text: text, font: font, constrainedSize: CGSize(width: width, height: .greatestFiniteMagnitude)).height } /// 基于 UILabel 获取高度(保留原接口) static func labelHeight(label: UILabel, width: CGFloat) -> CGFloat { return textHeight(text: label.text, font: label.font, width: width) } } ``` --- ### ✅ 使用示例 #### 1. 获取完整 bounds(适合 frame 布局) ```swift let bounds = LayoutCalculator.labelBounds(label: myLabel, maxWidth: 200) print("Width: \(bounds.width), Height: \(bounds.height)") ``` #### 2. 仅获取高度(兼容老逻辑) ```swift let height = LayoutCalculator.labelHeight(label: myLabel, width: 200) ``` #### 3.定义文本测试(无需 label 实例) ```swift let size = CGSize(width: 150, height: .greatestFiniteMagnitude) let rect = LayoutCalculator.textBounds(text: "这是一段很长的文字", font: UIFont.systemFont(ofSize: 17), constrainedSize: size) ``` --- ### ✅ 优势总结 | 特性 | 说明 | |------|------| | ✅ 统一管理 | 所有文本测量集中在一个文件 | | ✅ 功能完整 | 支持只求高、求完整 bounds、支持任意文本输入 | | ✅ 易扩展 | 后续可加行间距、富文本支持等 | | ✅ 可复用 | 多个模块都能导入使用 | | ✅ 类型安全 | 静态方法 + 参数明确,不易出错 | --- ### 🔁 原方法如何处理? 你可以安全删除原来的两个函数: ```swift // func getLabelViewHeight(...) // func getMultiLinesLabelViewBound(...) ``` 并替换为对应的 `LayoutCalculator.xxx` 调用。 > 💡 提示:可以用 Xcode 的 Find in Project 搜索原函数名,一次性全部替换。 --- ### 🧩 补充建议:命名一致性 你原来的方法名较长且风格偏 Objective-C(如 `getXXXViewBound`),建议逐步统一为 Swift 风格的简洁命名,例如: | 原名 | 推荐替代 | |------|---------| | `getLabelViewHeight` | `LayoutCalculator.labelHeight(...)` | | `getMultiLinesLabelViewBound` | `LayoutCalculator.labelBounds(...)` | 这样代码更现代、易读、易维护。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值