Set custom font on SwiftUI TextField - swiftui

How could I set a custom font for the TextFiled so I don't have to specify it each instance. Applying .font to the .textFieldStyle does not work.
VStack(alignment: .leading) {
Text("Oval Custom TextField Style").font(.title2)
HStack {
TextField("Search...", text: $text)
}
.textFieldStyle(OvalTextFieldStyle())
}.padding()

Try this:
HStack {
TextField("Country...", text: $country)
.textFieldStyle(TextFieldDarkFull())
TextField("Phone...", text: $phone)
.textFieldStyle(TextFieldDarkFull())
}
.font(.displaySmall())

Related

fixed text inside TextField SwiftUI

i want to put some fixed text inside TextField like "email: ....."
I tryed this:
HStack {
Image(systemName: "list.bullet").foregroundColor(.gray)
Text("email:")
TextField("", text: Binding(
get: { viewModel.email },
set: { viewModel.email = $0 }
))
.textFieldStyle(DefaultTextFieldStyle())
.frame(width: 60)
}
.padding()
.overlay(RoundedRectangle(cornerRadius: 10).stroke(
viewModel.emailValid() ? Color.gray : Color.red, lineWidth: 1
))
The problem is that this implementation only allows to click in specific part (inside the textField space) but i want to make all clickable to be more friendly to edit.
I tryed putting TextField and Text inside ZStack but the text entered by the user in inside te "Email text".
Maybe its possible to move the textField cursor to the end of the "email" text, or another implementation?
Thanks!
So if its mainly about activating the TextField on tap/click on any area of the element, you can use programmatic focus for TextField:
struct ContentView: View {
#State var input = ""
#FocusState var focused: Bool
var body: some View {
HStack {
Image(systemName: "list.bullet").foregroundColor(.gray)
Text("email:")
TextField("", text: $input)
.frame(width: 80)
.focused($focused)
}
.padding()
.overlay(RoundedRectangle(cornerRadius: 10).stroke(
true ? Color.gray : Color.red, lineWidth: 1
))
.contentShape(Rectangle()) // makes the whole element tappable
.onTapGesture {
focused = true // sets focus for textField
}
}
}

SwiftUI: How to align icon to left side of centered text field?

I've got a TextField in SwiftUI that is centered on the screen. I want to add a pencil icon immediately. to the left of it to indicate that it is editable - how can I do this? I've tried embedding both the TextField and Image in an HStack like this:
HStack {
Spacer()
Image(systemName: "pencil")
TextField(...)
}
But that only yields something like this:
where the textfield is no longer centered and the pencil is aligned to the left of the screen.
Any guidance is appreciated.
this should do it:
TextField("", text: $input)
.overlay(alignment: .leading) {
Image(systemName: "pencil")
.offset(x: -24, y: 0)
}
struct CustomTextFieldView: View {
#State private var text: String = ""
var body: some View {
VStack(alignment: .leading) {
textfeild
.padding()
}
}
var textfeild: some View {
HStack {
Image(systemName: "pencil")
TextField("Edit me!", text: $text)
}
.textFieldStyle(DefaultTextFieldStyle())
}
}
struct CustomTextFieldView_Previews: PreviewProvider {
static var previews: some View {
CustomTextFieldView()
}
}

Change Color of .navigationTitle in SwiftUI

Is there anyway to change the .navigationTitle color? I tried .foregroundColor and even passing the Text as a view but that did not work.
var body: some View {
VStack(alignment: .leading) {
Divider()
Spacer()
.navigationTitle(myList.name)
}.padding()
}
I also tried using the UINavigationBar appearance:
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

SwiftUI - Capsule fill till StrokeBorder

I'm trying to fill the capsuled strokeBorder but unfortunately it's filling the rectangle. See picture below:
Any ideas how I can make it fill till the strokeborder only? I'm wanting to use the field as search box which is why I want to use the textfield in it.
Here's my code:
struct ContentView: View {
var body: some View {
HStack{
TextField("Search", text: .constant(""))
}
.padding()
.frame(maxWidth:300)
.background(
Capsule()
.strokeBorder(Color.black,lineWidth: 0.8)
.background(Color.blue)
.clipped()
)
}
}
Many Thanks!
I think this is what you are after.
struct ContentView: View {
var body: some View {
HStack{
TextField("Search", text: .constant(""))
}
.padding()
.frame(maxWidth:300)
.background(
Capsule()
.strokeBorder(Color.black,lineWidth: 0.8)
.background(Color.blue)
.clipped()
)
.clipShape(Capsule())
}
}
Xcode preview of HStack with capsule border

Navigation title not appearing correctly in SwiftUI

I have a VStack wrapped around a NavigationView. I made a NavigationTitle by adding the modifier to VStack. However, my title is not appearing near the top of the screen as it should.
Here is my code:
NavigationView{
VStack{
Image(club.image)
.resizable()
.scaledToFit()
.frame(height: 300)
Text(club.name)
.font(.system(size: 40, weight: .black))
HStack(alignment: .center, spacing: 20){
Label(title: {
Text(club.league)
.foregroundColor(.secondary)
}, icon: {
Image(systemName: "location.north.circle.fill")
.foregroundColor(.blue)
})
Label(title: {
Text(club.netWorth)
.foregroundColor(.secondary)
}, icon: {
Image(systemName: "dollarsign.circle.fill")
.foregroundColor(.blue)
})
}
}.navigationTitle(club.name)
}
I have tried adding the '.navigationTitle' modifier to the NavigationView as well, but that isn't working.
Here is an image as well:
Navigation title image
Does anybody have a solution to this?
As lorem ipsum has mentioned. It's as simple as removing the extra NavigationView. When using a Navigation Link it's assumed that it's within a NavigationView. So you only need to declare it once in the root view. If you wanted additional NavigationLink you'd add it without a NavigationView.
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Chelsea")
NavigationLink("Second View Link", destination: SecondView())
}
.navigationTitle("Chelsea")
}
}
}
struct SecondView: View {
var body: some View {
VStack {
Text("Second View")
}
.navigationTitle("Second View")
}
}