Font.Weight异常:为何regular字重显示比medium更粗?
SwiftUI中Font.Weight .regular比.medium更粗的问题排查与解决
你遇到的问题是:使用SwiftUI的.system(.headline, design: .rounded)字体时,设置.fontWeight(.regular)的文本反而比.medium字重的文本更粗。
可能的原因
- 系统预设的
.headline样式自带默认字重,后续叠加.fontWeight()修饰符时,和预设值产生冲突,尤其针对.rounded这类特定字体设计,字重映射逻辑可能出现异常。 - SF Pro Rounded字体本身的字重层级映射,在结合系统文本样式时存在特殊表现,导致渲染结果不符合预期。
实用解决方案
1. 直接明确指定字体参数
跳过系统预设的.headline样式,直接定义字体大小、字重和设计风格,避免预设值干扰:
Text("Hello World! (Medium)") .font(.system(size: 17, weight: .medium, design: .rounded)) .foregroundColor(lightBodyText) Text("Hello World! (Regular)") .font(.system(size: 17, weight: .regular, design: .rounded)) .foregroundColor(lightBodyText)
(注:iOS中.headline的默认字号为17pt,可根据实际需求调整)
2. 使用自定义字体名称直接调用
直接指定SF Pro Rounded的具体字体文件名,确保字重渲染准确:
Text("Hello World! (Medium)") .font(.custom("SFProRounded-Medium", size: 17)) .foregroundColor(lightBodyText) Text("Hello World! (Regular)") .font(.custom("SFProRounded-Regular", size: 17)) .foregroundColor(lightBodyText)
3. 覆盖系统样式的默认字重
如果必须保留.headline样式,可通过.weight()方法直接覆盖字体的字重属性:
Text("Hello World! (Medium)") .font(.system(.headline, design: .rounded).weight(.medium)) .foregroundColor(lightBodyText) Text("Hello World! (Regular)") .font(.system(.headline, design: .rounded).weight(.regular)) .foregroundColor(lightBodyText)
内容的提问来源于stack exchange,提问作者Joseph
相关产品推荐
相关产品推荐

