作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 SwiftUI 中,您可以使用 tracking
View modifier 在 Text
View
上设置字体跟踪(字母间距):
Text("Hello, World!")
.tracking(10)
如果您有一个 Button
并应用您的自定义 ButtonStyle
,您可以对按钮的样式进行各种修改,但是由于 configuration.label
不是 Text
的实例,而是 ButtonStyleConfiguration.Label
的实例,我无法直接访问应用 tracking
。有没有一种方法可以做到这一点而不必直接在 Button
的 label
View
上设置它?这似乎是您应该能够做的事情,因为它是与样式相关的事情,但我不知道如何完成它。
public struct MyStyle: ButtonStyle {
public func makeBody(configuration: Configuration) -> some View {
configuration.label
// (if I tried to put `.tracking` here it wouldn't work because it isn't Text)
.foregroundColor(Color.red)
.clipShape(Capsule())
// … etc.
}
}
ViewModifier to change both font and tracking看起来~相关但没有相关答案。
最佳答案
是的,ButtonStyleConfiguration.Label
与Text
不匹配,但您可以忽略标准标签,并通过接口(interface)契约(Contract)请求自己的样式,这正是您需要的(在本例中为 Text
),如下面的演示
public struct MyStyle: ButtonStyle {
let label: Text // << here !!
public func makeBody(configuration: Configuration) -> some View {
label // << here !!
.tracking(10)
.foregroundColor(configuration.isPressed ? .black : .red)
.clipShape(Capsule())
}
}
并将其用作(或使用按钮构建器创建自己的按钮扩展以隐藏那些不需要的 curl )
Button(action: {
// action here
}){}
.buttonStyle(MyStyle(label: Text("Hello")))
使用 Xcode 13.2/iOS 15.2 测试
关于swiftui - 有没有办法在自定义 SwiftUI ButtonStyle 中设置字体跟踪(即间距)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70715341/
我是一名优秀的程序员,十分优秀!