How to Customize a Text in SwiftUI

I am trying to replicate a calculator app. I cant seem to modify my 'Text' view to look similarly.

Text("0")
   .background(Color.gray)
   .cornerRadius(10)
   .font(.largeTitle)

But its far from what I am trying to replicate. I tried offset, but it offsets the entire 'Text' view.

Basically I want my Text to look like what is pointed in the image

How to customize this area

Upvotes: 2

Views: 147

Answers (1)

Hrabovskyi Oleksandr
Hrabovskyi Oleksandr

Reputation: 3265

You can achieve this playing around with ZStack, VStack, HStack and Spacer(). Here is a quick example:

struct CalculatorText: View {

    var body: some View {

        ZStack {

            Rectangle()
                .cornerRadius(10)
                .foregroundColor(.gray)


            VStack {

                Spacer() // now the text will be on the bottom of ZStack

                HStack {

                    Spacer() // and now the text will be on the right side of ZStack

                    Text("0")
                        .bold()
                        .font(.system(size: 30))
                        .foregroundColor(.white)
                        .multilineTextAlignment(.trailing)
                        .padding()

                }
            }


        }
        .frame(height: 100)
        .padding()

    }

}

and the result will be: enter image description here

Upvotes: 3

Related Questions