UITextField和UITextView
来源:互联网 发布:知乎怎么加黑名单 编辑:程序博客网 时间:2024/05/17 03:34
UITextField
基本功能
import UIKitclass ViewController: UIViewController { var textField: UITextField! override func viewDidLoad() { super.viewDidLoad() textField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 31)) textField.borderStyle = .RoundedRect textField.contentVerticalAlignment = .Center textField.textAlignment = .Center textField.text = "Sir Richard Branson" textField.center = view.center view.addSubview(textField) } }
效果如下:
使用代理
下面的例子通过代理实现了一个统计字符数的功能,并且实时更新。
import UIKitclass ViewController: UIViewController, UITextFieldDelegate { var textField: UITextField! var label: UILabel! func calculateAndDisplayTextFieldLengthWithText(text: String){ var characterOrCharacters = "Character" if text.characters.count != 1{ characterOrCharacters += "s" } let stringLength = text.characters.count label.text = "\(stringLength) \(characterOrCharacters)" } func textField(paramTextField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{ guard let textFieldText = paramTextField.text else { return false } let text = NSString(string: textFieldText) let wholeText = text.stringByReplacingCharactersInRange( range, withString: string) calculateAndDisplayTextFieldLengthWithText(wholeText) return true } func textFieldShouldReturn(paramTextField: UITextField) -> Bool{ paramTextField.resignFirstResponder() return true } override func viewDidLoad() { super.viewDidLoad() textField = UITextField(frame: CGRect(x: 38, y: 30, width: 220, height: 31)) textField.delegate = self textField.borderStyle = .RoundedRect textField.contentVerticalAlignment = .Center textField.textAlignment = .Center textField.text = "Sir Richard Branson" view.addSubview(textField) label = UILabel(frame: CGRect(x: 38, y: 61, width: 220, height: 31)) view.addSubview(label) calculateAndDisplayTextFieldLengthWithText(textField.text!) }}
效果如下:
UITextView
基本功能
import UIKitclass ViewController: UIViewController { var textView: UITextView? override func viewDidLoad() { super.viewDidLoad() textView = UITextView(frame: view.bounds) if let theTextView = textView{ theTextView.text = "Some text goes here..." theTextView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0) theTextView.font = UIFont.systemFontOfSize(16) view.addSubview(theTextView) } } }
效果如下:
当点击文字时键盘会自动弹出,占了大概一半的空间。如果用户要输入大量的文字,键盘则会阻挡用户的输入。
下面的例子实现了这样的功能,当用户输到最后一行时,自动加载新的空行。
import UIKitclass ViewController: UIViewController { var textView: UITextView? let defaultContentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0) func handleKeyboardDidShow (notification: NSNotification){ /* Get the frame of the keyboard */ let keyboardRectAsObject = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue /* Place it in a CGRect */ var keyboardRect = CGRectZero keyboardRectAsObject.getValue(&keyboardRect) /* Give a bottom margin to our text view that makes it reach to the top of the keyboard */ textView!.contentInset = UIEdgeInsets(top: defaultContentInset.top, left: 0, bottom: keyboardRect.height, right: 0) } func handleKeyboardWillHide(notification: NSNotification){ textView!.contentInset = defaultContentInset } override func viewDidLoad() { super.viewDidLoad() textView = UITextView(frame: view.bounds) if let theTextView = textView{ theTextView.text = "Some text goes here..." theTextView.font = UIFont.systemFontOfSize(16) theTextView.contentInset = defaultContentInset view.addSubview(theTextView) NSNotificationCenter.defaultCenter().addObserver( self, selector: "handleKeyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver( self, selector: "handleKeyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil) } } deinit{ NSNotificationCenter.defaultCenter().removeObserver(self) } }
0 0
- UITextView 和 UITextField
- UITextField 和 UITextView
- UITextField和UITextView详解
- UITextField和UITextView
- UITextField和UITextView
- iOS- UITextField和UITextView区别
- UITextField和UITextView限制字数
- UITextView UITextField
- UITextView UITextField
- UITextField && UITextView
- iphone:圆角UITextView(和UITextField一样)
- iOS-UITextField和UITextView隐藏键盘
- UITextView 和 UITextField 的提示信息placeholder
- UITextField 和 UITextView实现字数限制
- UITextField和UITextView iOS中的输入框
- UITextView 和 UITextField 的提示信息placeholder
- 设置UITextField和UITextView的光标颜色
- UITextField和UITextView输入限制解决方案
- 切点表达式无法识别
- 无向图的双连通块(点的双连通)&&边的双连通算法
- Dom4j初步封装
- ORA-01033
- hdu 1712 ACboy needs your help 简单组合dp
- UITextField和UITextView
- 快速排序C++实现
- http协议简介
- Snail—UI学习之自定义通知NSNotification
- 怪盗基德的挑战书
- 无人机通信(WiFI/3G/4GLTE)
- 长连接为何要发送心跳包
- unix网络编程源码编译问题
- 同域iframe的高度自适应