Work together to create and grow together! This is the 30th day of my participation in the "Nuggets Daily New Plan · August Update Challenge", click to view the details of the event .
Background of the project
One day UI
he ran over and pointed and App页面
said:
You look at two identical buttons, why is the line segment of this button different from the line segment of the second button?
After I checked the code, I saw that when I copied the style code, I copied one line less, which is a bit embarrassing.
After this incident, I 假装反思
took a look. Should it be the UI设计
same as that, there 统一的样式
should be a unified style library in the system, so that there is no need to copy so many style codes every time?
Just do it.
Project construction
First, create a new SwiftUI
project named SwiftUIStyleSheet
.
basic style
Let's first take a look at how the basic style is designed, let's do a simple 文字按钮
example:
struct ContentView: View {
var body: some View {
Text("文如秋雨")
.font(.system(size: 17))
.foregroundColor(.white)
.padding()
.background(Color.blue)
.clipShape(Capsule())
}
}
复制代码
In the above code, we can see common modifiers for text buttons: font
font modifier, foregroundColor
font color modifier, padding
margin modifier, background
background color modifier, clipShape
shape drawing modifier.
view decorator
If App
all of the main buttons in this style use this style, we can extract the modifier part and build a ViewModifier
view modifier. The code structure is as follows:
struct MainTitle: ViewModifier {
func body(content: Content) -> some View {
content
//修饰符
}
}
复制代码
In the above code, we extract the original Text
modifier for the text button and build it into a new view modifier MainTitle
, our text button modifier will be modified content
, for example:
struct MainTitle: ViewModifier {
func body(content: Content) -> some View {
content
.font(.system(size: 17))
.foregroundColor(.white)
.padding()
.background(Color.blue)
.clipShape(Capsule())
}
}
复制代码
When we want to use, we just need to call modifier
the modifier, example:
struct ContentView: View {
var body: some View {
Text("文如秋雨")
.modifier(MainTitle())
}
}
复制代码
If we want to be more elegant and want to call the view modifier like a modifier, we ViewModifier
can also do something 拓展
, for example:
extension View {
func mainTitle() -> some View {
self.modifier(MainTitle())
}
}
复制代码
In this way, when we Text
add modifiers, we can use the mainTitle
view directly, for example:
struct ContentView: View {
var body: some View {
Text("文如秋雨")
.mainTitle()
}
}
复制代码
modifier action
Maybe one button can't see the effect, let's create one more button to see:
struct ContentView: View {
var body: some View {
VStack(spacing:15){
Text("文如秋雨")
.mainTitle()
Text("文如秋雨")
.mainTitle()
Text("文如秋雨")
.mainTitle()
Text("文如秋雨")
.mainTitle()
}
}
}
复制代码
When different pages in our application use the same button style, if we want to modify the style uniformly, we only need to modify the ViewModifier
view decorator, for example:
struct MainTitle: ViewModifier {
func body(content: Content) -> some View {
content
.frame(maxWidth:.infinity)
.font(.system(size: 17))
.foregroundColor(.white)
.padding()
.background(LinearGradient(gradient: Gradient(colors: [Color.blue, Color.green]), startPoint: .leading, endPoint: .trailing))
.cornerRadius(8)
.padding(.horizontal)
}
}
复制代码
Congratulations, you have completed the entire content of this chapter!
Come and try it.
If this column is helpful to you, please like, comment, and follow~