Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdrk18/bdbaabd4ba898dcbdb0d8e5c62fbabad to your computer and use it in GitHub Desktop.
Save tdrk18/bdbaabd4ba898dcbdb0d8e5c62fbabad to your computer and use it in GitHub Desktop.
alignment right 属性を持つ TextField で右端のスペースを表示できるようにする
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
private var myTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let width: CGFloat = 200
let height: CGFloat = 30
let x: CGFloat = (self.view.bounds.width - width) / 2
let y: CGFloat = (self.view.bounds.height - height) / 2
myTextField = UITextField(frame: CGRect(x:x, y:y, width:width, height:height))
myTextField.text = "Hello"
myTextField.delegate = self
myTextField.borderStyle = .roundedRect
myTextField.textAlignment = .right
self.view.addSubview(myTextField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
}
func textFieldDidEndEditing(_ textField: UITextField) {
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text = textField.text!
if range.location == text.characters.count && string == " " {
textField.text = text + "\u{00a0}"
return false
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment