To incorporate a mask into a Swift UITextField, you can use the MaskedTextField library. Here are the steps to do it:
Install the MaskedTextField library in your project using Cocoapods or manually.
Import MaskedTextField in your view controller.
import MaskedTextField
let maskedTextField = MaskedTextField(frame: CGRect(x: 50, y: 50, width: 200, height: 30))
view.addSubview(maskedTextField)
maskedTextField.maskExpression = "(###) ###-####"
This will create a text field that accepts only 10 digits in the format of (123) 456-7890.
maskedTextField.backgroundColor = .white
maskedTextField.textColor = .black
maskedTextField.font = UIFont.systemFont(ofSize: 16)
let phoneNumber = maskedTextField.text
This will give you the text entered by the user in the mask pattern format.
By using MaskedTextField, you can easily create text fields that accept input in a specific format such as phone numbers, email addresses, zip codes, etc.
Asked: 2021-10-06 11:00:00 +0000
Seen: 9 times
Last updated: Sep 01 '22