Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make TextField align right (trailing)

Tags:

ios

swift

swiftui

I am trying to have the value of a TextField displayed with trailing alignment.

As you can see the value 34.3 is display with leading alignment.

I am sure I am missing something obvious but I can't figure out what. Any ideas?

@State private var endwert = 34.3  var numberFormatter: NumberFormatter {     let formatter = NumberFormatter()     formatter.numberStyle = .decimal     formatter.locale = Locale.current     return formatter }  ...  HStack {    Text("Endwert")     .frame(width: nil, height: nil, alignment: .topTrailing)     .background(Color .green)    Spacer()    TextField($endwert, formatter: numberFormatter)     .background(Color.yellow)     .frame(width: nil, height: nil, alignment: .trailing)    Text("m2") } 

wich looks like this

like image 723
mimo Avatar asked Jun 11 '19 21:06

mimo


1 Answers

Add .multilineTextAlignment(.trailing) to your TextField:

TextField($endwert, formatter: numberFormatter)   .multilineTextAlignment(.trailing)   ... 

Which results in:

enter image description here

like image 146
M Reza Avatar answered Oct 07 '22 05:10

M Reza