SwiftUI

SwiftUI实战-TextField限制输入最大字数

2024-03-27  本文已影响0人  ISwiftUI
import SwiftUI
import Combine

let maxCharacterLength: Int = 6

struct ContentView: View {
    @State private var text: String = ""
    var body: some View {
        TextField("", text: $text)
            .background(Color.orange)
            .onReceive(Just(text)) { _ in
                if(text.count > maxCharacterLength) {
                    text =  String(text.prefix(maxCharacterLength))
                }
            }
    }
}
上一篇下一篇

猜你喜欢

热点阅读