Pat*_*oda 1 fonts swift swiftui
我正在尝试在我的SwiftUI项目中使用SF舍入字体,您将如何设置它?
我已经试过弄乱.font()了,但是没有用(我无法将其设置为这种圆形字体)
小智 29
Text("Your Text").font(.system(.body, design: .rounded))
Run Code Online (Sandbox Code Playgroud)
小智 8
我想添加一个额外的用例:如果您想将自定义字体粗细应用于文本字段之类的圆角 SF 字体,同时仍然保持动态字体缩放,您可以执行以下操作:
TextField("test", $test)
.font(Font.system(.largeTitle, design: .rounded).weight(.heavy))
Run Code Online (Sandbox Code Playgroud)
这是必需的,因为无法.fontWeight()
直接调用 a TextField
(至少从 iOS 13 开始)。
我知道问题与SwiftUI有关,但我认为也包含UIKit答案可能会有所帮助。
let fontSize: CGFloat = 24
// Here we get San Francisco with the desired weight
let systemFont = UIFont.systemFont(ofSize: fontSize, weight: .regular)
// Will be SF Compact or standard SF in case of failure.
let font: UIFont
if let descriptor = systemFont.fontDescriptor.withDesign(.rounded) {
font = UIFont(descriptor: descriptor, size: fontSize)
} else {
font = systemFont
}
Run Code Online (Sandbox Code Playgroud)
将@Pomme2Poule 的 UIKit 答案转换为易于使用的函数,如果有人需要的话。函数也使用动态类型,所以它会随着字体大小而缩放。
func roundedFont(ofSize style: UIFont.TextStyle, weight: UIFont.Weight) -> UIFont {
// Will be SF Compact or standard SF in case of failure.
let fontSize = UIFont.preferredFont(forTextStyle: style).pointSize
if let descriptor = UIFont.systemFont(ofSize: fontSize, weight: weight).fontDescriptor.withDesign(.rounded) {
return UIFont(descriptor: descriptor, size: fontSize)
} else {
return UIFont.preferredFont(forTextStyle: style)
}
}
Run Code Online (Sandbox Code Playgroud)
这样做对我来说适用于以下Text
对象SwiftUI
:
.font(.system(size: 13.0, weight: .regular, design: .rounded))
归档时间: |
|
查看次数: |
889 次 |
最近记录: |