38 Providing a character limit on UITextField

Swift3.0 // // ViewController.swift // UIKit038_3.0 // // Created by KimikoWatanabe on 2016/08/18. // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit class ViewController: UIViewController, UITextFieldDelegate { override func viewDidLoad() { super.viewDidLoad() // 背景を緑色に設定. self.view.backgroundColor = UIColor.green // UITextFieldを生成. let myTextField: UITextField = UITextField(frame: CGRect(x:0, y:0, width:200, height:30)) // 最初に表示する文字. myTextField.text = "Hi" // Delegateを設定. myTextField.delegate = self // 枠の線を表示. myTextField.borderStyle = UITextBorderStyle.roundedRect // UITextFieldの表示する位置. myTextField.layer.position = CGPoint(x:self.view.bounds.width/2, y:100) // TextViewをviewに追加する. self.view.addSubview(myTextField) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } /* テキストが編集された際に呼ばれる. */ func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { // 文字数最大を決める. let maxLength: Int = 6 // 入力済みの文字と入力された文字を合わせて取得. let str = textField.text! + string // 文字数がmaxLength以下ならtrueを返す. if str.characters.count < maxLength { return true } print("6文字を超えています") return false } } Swift 2.3 // // ViewController.swift // UIKit038_2.3 // // Created by KimikoWatanabe on 2016/08/18. // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit class ViewController: UIViewController, UITextFieldDelegate { override func viewDidLoad() { super.viewDidLoad() // 背景を緑色に設定. self.view.backgroundColor = UIColor.greenColor() // UITextFieldを生成. let myTextField: UITextField = UITextField(frame: CGRectMake(0, 0, 200, 30)) // 最初に表示する文字. myTextField.text = "Hi" // Delegateを設定. myTextField.delegate = self // 枠の線を表示. myTextField.borderStyle = UITextBorderStyle.RoundedRect // UITextFieldの表示する位置. myTextField.layer.position = CGPointMake(self.view.bounds.width/2, 100) // TextViewをviewに追加する. self.view.addSubview(myTextField) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } /* テキストが編集された際に呼ばれる. */ func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { // 文字数最大を決める. let maxLength: Int = 6 // 入力済みの文字と入力された文字を合わせて取得. let str = textField.text! + string // 文字数がmaxLength以下ならtrueを返す. if str.characters.count < maxLength { return true } print("6文字を超えています") return false } } 2.3と3.0の差分 UIColorの参照方法が変更(UIColor.grayColor()->UIColor.gray) CGRect,CGPointの初期化方法の変更(CGRectMake,CGPointMakeの廃止) UITextFieldDelegateのデリゲートメソッドの引数が変更 UITextBorderStyleのプロパティ名が一部変更(RoundedRect->roundedRect)

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.