Skip to content

Instantly share code, notes, and snippets.

@jimbrayrcp
Created June 10, 2020 19:18
Show Gist options
  • Save jimbrayrcp/63b1db39df32830bc61c947a36006106 to your computer and use it in GitHub Desktop.
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.
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