Created
December 16, 2019 12:49
-
-
Save DaisukeNagata/5002c49061f18d72e0a40dfda1290b1a to your computer and use it in GitHub Desktop.
SwiftUI_TextField_Part1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Author
DaisukeNagata
commented
Dec 16, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment