swift中UITextView的使用
来源:互联网 发布:高中生听歌学英语软件 编辑:程序博客网 时间:2024/06/06 04:38
- let textview = UITextView(frame: CGRectMake(10.0, 10.0, (CGRectGetWidth(self.view.bounds) - 10.0 * 2), 80.0))
- self.view.addSubview(textview)
-
- textview.backgroundColor = UIColor.lightGrayColor()
-
- textview.textAlignment = NSTextAlignment.Left
- textview.textColor = UIColor.redColor()
- textview.font = UIFont(name: "GillSans", size: 15.0)
-
- textview.tintColor = UIColor.greenColor()
-
- textview.editable = true
- textview.userInteractionEnabled = true
- textview.scrollEnabled = true
- textview.showsHorizontalScrollIndicator = true
- textview.showsVerticalScrollIndicator = true
-
- textview.delegate = self
-
- class ViewController: UIViewController, UITextViewDelegate {
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- }
- }
-
-
-
-
- func textViewShouldBeginEditing(textView: UITextView) -> Bool {
- print("1 textViewShouldBeginEditing")
-
- return true
- }
-
- func textViewDidBeginEditing(textView: UITextView) {
- print("2 textViewDidBeginEditing")
- }
-
- func textViewDidChange(textView: UITextView) {
- print("3 textViewDidChange")
- }
-
- func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
- print("4 textView")
-
- print("text:\(textView.text) length = \(textView.text?.characters.count)")
-
-
- if text == "\n"
- {
-
-
-
-
- textView.resignFirstResponder()
-
- return true
- }
- return true
- }
-
- func textViewShouldEndEditing(textView: UITextView) -> Bool {
- print("5 textViewShouldEndEditing")
-
- return true
- }
-
- func textViewDidEndEditing(textView: UITextView) {
- print("6 textViewDidEndEditing")
- }
-
- textview.keyboardType = UIKeyboardType.WebSearch
- textview.returnKeyType = UIReturnKeyType.Done
-
-
-
-
-
-
-
- let accessoryview = UIView(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 40.0))
- accessoryview.backgroundColor = UIColor.greenColor()
- let accessoryLeft = UIButton(frame: CGRectMake(10.0, 10.0, 60.0, 20.0))
- accessoryview.addSubview(accessoryLeft)
- accessoryLeft.setTitle("取消", forState: UIControlState.Normal)
- accessoryLeft.backgroundColor = UIColor.orangeColor()
- accessoryLeft.addTarget(self, action: Selector("leftClick:"), forControlEvents: UIControlEvents.TouchUpInside)
- let accessoryRight = UIButton(frame: CGRectMake((CGRectGetWidth(accessoryview.bounds) - 10.0 - 60.0), 10.0, 60.0, 20.0))
- accessoryview.addSubview(accessoryRight)
- accessoryRight.setTitle("确定", forState: UIControlState.Normal)
- accessoryRight.backgroundColor = UIColor.orangeColor()
- accessoryRight.addTarget(self, action: Selector("rightClick:"), forControlEvents: UIControlEvents.TouchUpInside)
- textview.inputAccessoryView = accessoryview
-
-
- func click(button:UIButton)
- {
- self.view.endEditing(true)
- }
-
-
- func leftClick(button:UIButton)
- {
- print("取消")
- }
-
- func rightClick(button:UIButton)
- {
- self.view.endEditing(true)
- print("确定")
- }
-
-
-
-
-
-
- NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textViewEditChanged:"), name: UITextViewTextDidChangeNotification, object: textview)
-
-
- func textViewEditChanged(notification:NSNotification)
- {
- let textView:UITextView! = notification.object as! UITextView
- if (textView != nil)
- {
- let text:String! = textView.text
- let length = text.characters.count
- if (length > 200)
- {
- textView.text = text.substringToIndex(text.startIndex.advancedBy(200))
- }
- }
- }
0 0