如何为SwiftUI中的文本(...)组件创建基于枚举的.textstyle(.title)修饰符?
我想以类似的方式实现文本的修改器设置,因为它已经存在于按钮。
又名:
Button( ... )
.buttonStyle(.plain) // <-- .plain and not PlainStyle()
问题
当然,我不能使用不相同的不透明。如果是 view
,我可以将其包装在 anyview
中,但是对于 view> viewModifier
s,我需要另一个解决方案。
错误:函数声明不透明的返回类型,但其身体中的返回语句没有匹配的基础类型
,这可能是一个奖励想法,它是一个 .textstyle(.title)(.title)
修改器,但在我的中眼睛,它可以减少我的代码以大量写作。
来源
struct TitleStyle: ViewModifier {
func body(content: Content) -> some View {
...
}
}
struct BodyStyle: ViewModifier {
func body(content: Content) -> some View {
...
}
}
enum TextStyle {
case title
case body
// Error: Function declares an opaque return type,
// but the return statements in its body do not have matching underlying types
var modifier: some ViewModifier {
switch self
{
case .title: return TitleStyle()
case .body: return BodyStyle()
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它的工作方式不同。由于所有这些都是围绕仿制药,因此我们需要限制已知具体类型的声明。
因此,具有
titlestyle
和BodyStyle
声明和具体,我们可以指定并声明上述使用的扩展名,
因此我们可以作为Demo
使用Xcode 13.4/ios 15.5
It works different way. As all this is around generics we need to restrict declarations for known concrete types.
So, having
TitleStyle
andBodyStyle
declared and concrete, we can specifyand then declare extension to use above like
so as a result we can do as demo
Prepared with Xcode 13.4 / iOS 15.5
Test module in GitHub