Created
June 10, 2020 19:18
-
-
Save jimbrayrcp/63b1db39df32830bc61c947a36006106 to your computer and use it in GitHub Desktop.
For Swift UI: a simple text field character counter with return value into another text field.
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 | |
import Combine | |
class TextCountMgr: ObservableObject { | |
@Published var counted = "0" | |
@Published var text = "" { | |
didSet { | |
counted = String(text.count) | |
} | |
} | |
} | |
struct TextCounterTestView: View { | |
@ObservedObject var textCountMgr = TextCountMgr() | |
var body: some View { | |
VStack { | |
VStack { | |
TextField("Placeholder", text: $textCountMgr.text) | |
} | |
.padding(.all) | |
.background(Color.blue) | |
.foregroundColor(.white) | |
VStack { | |
Text("\(textCountMgr.counted)") | |
} | |
} | |
} | |
} | |
struct TextCounterTestView_Previews: PreviewProvider { | |
static var previews: some View { | |
TextCounterTestView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment