Home > AI > IOS > SwiftUI >

(Effect)text with rounded background

ContentView.swift

struct ContentView: View {

    
    @State var defaultPadding: CGFloat = 0
    
   
    var body: some View {
        VStack {
            GetDefaultPaddingView(defaultPadding: $defaultPadding)
            
            GeometryReader {geomtry in
                Text("综合")
                    .foregroundColor(.white)
                    .padding(.top, self.defaultPadding/2)
                    .padding(.bottom, self.defaultPadding/2)
                    .padding([.leading, .trailing])
                    .background(RoundedRectangle(cornerRadius: geomtry.size.height/2)
                                    .foregroundColor(.blue))
            }
        }
    }

GetDefaultPaddingView.swift

import SwiftUI

struct GetDefaultPaddingView: View {
    @Binding var defaultPadding: CGFloat
    @State var isHidden: Bool = false
    
    var body: some View {
        if !isHidden {
            Rectangle()
                .frame(width: 0, height: 0)
                .padding(.top)
                .background(GeometryReader{geometry in
                    Rectangle()
                        .onAppear{
                            self.defaultPadding = geometry.size.height
                            self.isHidden = true
                        }
                })
        }
    }
}
Relevant tags:

Leave a Reply