Created
August 3, 2017 06:46
-
-
Save tdrk18/bdbaabd4ba898dcbdb0d8e5c62fbabad to your computer and use it in GitHub Desktop.
alignment right 属性を持つ TextField で右端のスペースを表示できるようにする
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 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