Skip to content

Instantly share code, notes, and snippets.

@ylem
Last active April 10, 2023 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ylem/5013d9b1e3b7e48a53cc69f6c94eb0c2 to your computer and use it in GitHub Desktop.
Save ylem/5013d9b1e3b7e48a53cc69f6c94eb0c2 to your computer and use it in GitHub Desktop.
InputTextView with indicator border
import SwiftUI
@available(iOS 15.0, *)
struct InputTextView: View {
private let placeholder: String?
private let text: Binding<String>
private let isError: Binding<Bool>
@FocusState private var isFocused: Bool
init(
placeholder: String? = nil,
text: Binding<String>,
isError: Binding<Bool>
) {
self.placeholder = placeholder
self.text = text
self.isError = isError
}
private var borderColor: Color {
if isFocused {
return Color.gray
} else if isError.wrappedValue {
return Color.red
} else {
return Color.blue
}
}
var body: some View {
TextField("", text: text)
.font(.body)
.padding(.vertical, 12)
.padding(.leading, 8)
.focused($isFocused)
.overlay(RoundedRectangle(cornerRadius: 4).stroke(borderColor, lineWidth:1))
.padding(.bottom, 16)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment