Created
May 11, 2023 09:30
-
-
Save djryanash/8341491e35d05b11363b1353b10c846b to your computer and use it in GitHub Desktop.
UILabel updates automatically when UITextField is updated in UIKit (surprisingly hard to do).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class HomeViewController: UIViewController, UITextFieldDelegate { | |
let style = UITraitCollection().userInterfaceStyle | |
var textField: UITextField = { | |
let textField = UITextField() | |
textField.translatesAutoresizingMaskIntoConstraints = false | |
textField.backgroundColor = .systemBackground | |
textField.placeholder = "Enter some text here" | |
textField.adjustsFontForContentSizeCategory = true | |
return textField }() | |
var label: UILabel = { | |
let label = UILabel() | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.backgroundColor = .systemBackground | |
label.text = "This is a label." | |
label.adjustsFontForContentSizeCategory = true | |
return label }() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = .secondarySystemBackground | |
textField.delegate = self | |
view.addSubview(textField) | |
view.addSubview(label) | |
addConstraints() | |
} | |
func addConstraints() { | |
textField.topAnchor.constraint( | |
equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true | |
textField.leadingAnchor.constraint( | |
equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: self.view.bounds.width / 10).isActive = true | |
textField.trailingAnchor.constraint( | |
equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -(self.view.bounds.width / 10)).isActive = true | |
textField.textColor = { | |
if self.traitCollection.userInterfaceStyle.rawValue == 2 { return .white } | |
else { return .black } }() | |
label.topAnchor.constraint( | |
equalTo: textField.bottomAnchor, constant: 10).isActive = true | |
label.leadingAnchor.constraint( | |
equalTo: textField.leadingAnchor).isActive = true | |
label.trailingAnchor.constraint( | |
equalTo: textField.trailingAnchor).isActive = true | |
label.textColor = { | |
if self.traitCollection.userInterfaceStyle.rawValue == 2 { return .white } | |
else { return .black } }() | |
} | |
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { | |
let updatedText = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) | |
if string == "" && range.length == 1 && label.text == label.text && label.text != updatedText { | |
label.text = updatedText | |
} else { | |
label.text = textField.text | |
} | |
return true | |
} | |
func textFieldDidBeginEditing(_ textField: UITextField) { | |
if label.text != "" { | |
label.text = " " | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment