Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Created December 16, 2019 12:49
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 DaisukeNagata/5002c49061f18d72e0a40dfda1290b1a to your computer and use it in GitHub Desktop.
Save DaisukeNagata/5002c49061f18d72e0a40dfda1290b1a to your computer and use it in GitHub Desktop.
SwiftUI_TextField_Part1
import SwiftUI
struct ContentView: View {
@State var text: String = ""
@State var text2: String = ""
@State var spacing: CGFloat = 0
@State var didTap = false
var body: some View {
VStack {
HStack(alignment: .bottom, spacing: spacing) {
TextField("placeholder", text: $text)
text.isEmpty == false ?
HorizontalLine(color: didTap ? Color.red : Color.black) :
HorizontalLine(color: didTap ? Color.black : Color.red)
// Write something
TextField("placeholder2", text: $text2)
text2.isEmpty == false ?
HorizontalLine(color: didTap ? Color.red : Color.black) :
HorizontalLine(color: didTap ? Color.black : Color.red)
// Write something
}
}.position(.init(x: UIScreen.main.bounds.width/2+50, y: UIScreen.main.bounds.height/2))
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct HorizontalLine: View {
private var color: Color? = nil
private var height: CGFloat = 1.0
private var shape: HorizontalLineShape?
init(color: Color, height: CGFloat = 1.0) {
self.color = color
self.height = height
}
var body: some View {
HorizontalLineShape().fill(self.color!).frame(minWidth: 0, maxWidth: .infinity, minHeight: height, maxHeight: height)
}
}
struct HorizontalLineShape: Shape {
func path(in rect: CGRect) -> Path {
let fill = CGRect(x: -rect.size.width, y: 0, width: rect.size.width, height: rect.size.height)
var path = Path()
path.addRoundedRect(in: fill, cornerSize: CGSize(width: 2, height: 2))
return path
}
}
@DaisukeNagata
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment