swift中UITextView的使用

来源:互联网 发布:高中生听歌学英语软件 编辑:程序博客网 时间:2024/06/06 04:38
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. let textview = UITextView(frame: CGRectMake(10.010.0, (CGRectGetWidth(self.view.bounds) - 10.0 * 2), 80.0))  
  2. self.view.addSubview(textview)  
  3.           
  4. textview.backgroundColor = UIColor.lightGrayColor()  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 字体设置  
  2. textview.textAlignment = NSTextAlignment.Left  
  3. textview.textColor = UIColor.redColor()  
  4. textview.font = UIFont(name: "GillSans", size15.0)  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 光标颜色  
  2. textview.tintColor = UIColor.greenColor()  
  3.           
  4. textview.editable = true  
  5. textview.userInteractionEnabled = true  
  6. textview.scrollEnabled = true  
  7. textview.showsHorizontalScrollIndicator = true  
  8. textview.showsVerticalScrollIndicator = true  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 代理,注意添加代理协议,及实现代理方法  
  2. textview.delegate = self  
  3. // 添加协议  
  4. class ViewController: UIViewController, UITextViewDelegate {  
  5.   
  6.     override func viewDidLoad() {  
  7.         super.viewDidLoad()  
  8.   
  9.    }  
  10. }  
  11.   
  12. // 实现代理方法  
  13. // MARK: - UITextViewDelegate  
  14.       
  15. func textViewShouldBeginEditing(textView: UITextView) -> Bool {  
  16.         print("1 textViewShouldBeginEditing")  
  17.           
  18.         return true  
  19. }  
  20.       
  21. func textViewDidBeginEditing(textView: UITextView) {  
  22.         print("2 textViewDidBeginEditing")  
  23. }  
  24.       
  25. func textViewDidChange(textView: UITextView) {  
  26.         print("3 textViewDidChange")  
  27. }  
  28.       
  29. func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {  
  30.         print("4 textView")  
  31.           
  32.         print("text:\(textView.text) length = \(textView.text?.characters.count)")  
  33.           
  34.         // 回车时退出编辑  
  35.         if text == "\n"  
  36.         {  
  37. //            textView.endEditing(true)  
  38.             // 或  
  39. //            self.view.endEditing(true)  
  40.             // 或  
  41.             textView.resignFirstResponder()  
  42.               
  43.             return true  
  44.         }  
  45.         return true  
  46. }  
  47.       
  48. func textViewShouldEndEditing(textView: UITextView) -> Bool {  
  49.         print("5 textViewShouldEndEditing")  
  50.           
  51.         return true  
  52. }  
  53.       
  54. func textViewDidEndEditing(textView: UITextView) {  
  55.         print("6 textViewDidEndEditing")  
  56. }  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 输入设置  
  2. textview.keyboardType = UIKeyboardType.WebSearch  
  3. textview.returnKeyType = UIReturnKeyType.Done  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 自定义输入源控件  
  2. // let inputview = UIButton(frame: CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 100.0))  
  3. // inputview.setImage(UIImage(named: "normalImage"), forState: UIControlState.Normal)  
  4. // inputview.backgroundColor = UIColor.lightGrayColor()  
  5. // inputview.addTarget(self, action: Selector("click:"), forControlEvents: UIControlEvents.TouchUpInside)  
  6. // textview.inputView = inputview  
  7. // 自定义输入源控件副视图  
  8. let accessoryview = UIView(frame: CGRectMake(0.00.0, CGRectGetWidth(self.view.bounds), 40.0))  
  9. accessoryview.backgroundColor = UIColor.greenColor()  
  10. let accessoryLeft = UIButton(frame: CGRectMake(10.010.060.020.0))  
  11. accessoryview.addSubview(accessoryLeft)  
  12. accessoryLeft.setTitle("取消", forState: UIControlState.Normal)  
  13. accessoryLeft.backgroundColor = UIColor.orangeColor()  
  14. accessoryLeft.addTarget(self, action: Selector("leftClick:"), forControlEvents: UIControlEvents.TouchUpInside)  
  15. let accessoryRight = UIButton(frame: CGRectMake((CGRectGetWidth(accessoryview.bounds) - 10.0 - 60.0), 10.060.020.0))  
  16. accessoryview.addSubview(accessoryRight)  
  17. accessoryRight.setTitle("确定", forState: UIControlState.Normal)  
  18. accessoryRight.backgroundColor = UIColor.orangeColor()  
  19. accessoryRight.addTarget(self, action: Selector("rightClick:"), forControlEvents: UIControlEvents.TouchUpInside)  
  20. textview.inputAccessoryView = accessoryview  
  21.   
  22. // MARK: - click  
  23. func click(button:UIButton)  
  24. {  
  25.         self.view.endEditing(true)  
  26. }  
  27.       
  28. // MARK: - left/right click  
  29. func leftClick(button:UIButton)  
  30. {  
  31.         print("取消")  
  32. }  
  33.       
  34. func rightClick(button:UIButton)  
  35. {  
  36.         self.view.endEditing(true)  
  37.         print("确定")  
  38. }  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 其他  
  2. // 第一响应,即进入编辑状态  
  3. // textview.becomeFirstResponder()  
  4. // 结束响应,即结束编辑  
  5. // textview.resignFirstResponder()  
  6. // 发通知-输入改变  
  7. NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textViewEditChanged:"), name: UITextViewTextDidChangeNotification, object: textview)  
  8.   
  9. // MARK: - 通知响应方法  
  10. func textViewEditChanged(notification:NSNotification)  
  11. {  
  12.         let textView:UITextView! = notification.object as! UITextView  
  13.         if (textView != nil)  
  14.         {  
  15.             let text:String! = textView.text  
  16.             let length = text.characters.count  
  17.             if (length > 200)  
  18.             {  
  19.                 textView.text = text.substringToIndex(text.startIndex.advancedBy(200))  
  20.             }  
  21.         }  
  22. }  

 
0 0
原创粉丝点击