I have a UITextField using the system font with kerning applied.
Problem
- When adding leading whitespace characters to the empty field, the width of each whitespace character is significantly smaller than it should be.
- On adding a non-whitespace character after all leading whitespaces, the width of each whitespace character returns to the expected size.
- When deleting all characters added after leading whitespace characters, the text field returns to the problem state from #1.
The result of all this is that the field's caret appears to move erratically between state #2 and the other states.
Code
Adding the text field via a Storyboard will cause the app to hang when running in Simulator. Clicking the Debug View Hierarchy button while the app is running may produce an "Unable to Capture View Hierarchy" error.
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField! {
didSet {
textField.defaultTextAttributes = [.kern: 1]
}
}
}
Therefore, add the text field in code to produce the behavior shown below:
class ViewController: UIViewController {
let textField: UITextField = {
let textField = UITextField()
textField.defaultTextAttributes = [.kern: 1]
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(textField)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
textField.frame.size = CGSize(width: 280, height: 44)
textField.center = view.center
}
}
Video
