[Xcode/Swift] 全角入力絶対許さないマンなTextFieldを作る方法

UIKit (RxSwift使用)

halfWidthTextField.rx.text.orEmpty
    .map { $0.applyingTransform(.fullwidthToHalfwidth, reverse: false) }
    .bind(to: halfWidthLabel.rx.text)
    .disposed(by: disposeBag)

SwiftUI

import SwiftUI

struct ContentView: View {
    @State private var inputText: String = ""
    @State private var transformedText: String = ""

    var body: some View {
        VStack(spacing: 20) {
            TextField("Enter text", text: $inputText)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .padding()
                .onChange(of: inputText) { newValue in
                    transformedText = newValue.applyingTransform(.fullwidthToHalfwidth, reverse: false) ?? newValue
                }

            Text("Transformed Text:")
                .font(.headline)
            Text(transformedText)
                .foregroundColor(.blue)
        }
        .padding()
    }
}