iOS开发——UI篇Swift篇&UITextView

时间:2024-04-21 06:33:29

UITextView

一:UITextView使用及其属性的设置

  titleLabel.text = titleString

         //创建UITextView对象
         textView = UITextView(frame:CGRectMake(10.0, 120.0, 300.0, 200.0))

         //为方便看到效果,设置背景颜色
         textView.backgroundColor = UIColor.grayColor()

         //添加到视图上
         self.view.addSubview(textView)

         //------------------ 常用属性

         //设置textview里面的字体颜色
         textView.textColor = UIColor.greenColor()

         //设置文本字体
         textView.font = UIFont.systemFontOfSize();//使用系统默认字体,指定14号字号
         textView.font = UIFont(name: )//指定字体,指定字号

         //设置它的背景颜色
         textView.backgroundColor = UIColor.grayColor()

         //设置显示内容
         textView.text = "经常听到:\n 被中介骗了,押金不退,晚一天交房租,被讹了。\n\n租房普遍现象:\n网上报价不真实?经常被忽悠!(看房时报价都比网上高!)\n证件不齐全,被骗过!(其实根本不是房东啦!)\n看房前态度都很热情!\n签约之后态度骤变!\n入住后家电维修只能靠自己!\n房屋到期,押金各种被勒索!\n\n现在开始,你来改变这一切!\n《租房点评》为你而备,租房无忧!\n\n再也不用担心被欺骗,想要知道给你介绍房子的人好不好,《租房点评》告诉你!"

         //文本对齐方式
         textView.textAlignment = NSTextAlignment.Right

         //文本视图设置圆角
         textView.layer.cornerRadius = 

         //是否允许点击链接和附件
         textView.selectable = true

         //返回键的类型
         textView.returnKeyType = UIReturnKeyType.Done

         //键盘类型
         textView.keyboardType = UIKeyboardType.Default

         //是否可以滚动
         textView.scrollEnabled = true

         //自适应高度
         textView.autoresizingMask = UIViewAutoresizing.FlexibleHeight

         //设置富文本
         var attributeString:NSMutableAttributedString=NSMutableAttributedString(string: "经常听到:\n 被中介骗了,押金不退,晚一天交房租,被讹了。\n\n租房普遍现象:\n网上报价不真实?经常被忽悠!(看房时报价都比网上高!)\n证件不齐全,被骗过!(其实根本不是房东啦!)\n看房前态度都很热情!\n签约之后态度骤变!\n入住后家电维修只能靠自己!\n房屋到期,押金各种被勒索!\n\n现在开始,你来改变这一切!\n《租房点评》为你而备,租房无忧!\n\n再也不用担心被欺骗,想要知道给你介绍房子的人好不好,《租房点评》告诉你!")

         //设置字体颜色
         attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSMakeRange(, attributeString.length))

         //文本所有字符字体HelveticaNeue-Bold,16号
         attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: )!, range: NSMakeRange(, attributeString.length))

         //文本0开始5个字符字体HelveticaNeue-Bold,16号
         attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: )!, range: NSMakeRange(, ))

         //设置字体颜色
         attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(, ))

         //设置文字背景颜色
         attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.orangeColor(), range: NSMakeRange(, ))

         //赋值富文本
         textView.attributedText = attributeString

         //选中一段文本
         textView.becomeFirstResponder()
         textView.selectedRange = NSMakeRange(, )

         //获取内容整体高度
         var height:CGFloat = textView.contentSize.height;

         //-------------------------------------------自定义菜单

         //定义4个菜单选择
         var menuItem1:UIMenuItem = UIMenuItem(title: "分享到微信", action: "shareWXMenu:")
         var menuItem2:UIMenuItem = UIMenuItem(title: "分享到微博", action: "shareWBMenu:")

         //获取菜单控制器
         var menuController:UIMenuController = UIMenuController.sharedMenuController()
         menuController.menuItems = [menuItem1,menuItem2]

         //-------------------------------------------指定代理
         textView.delegate = self

         //-------------------------------------------添加通知
         //文本框开始编辑时,触发
         NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidBeginEditing", name: UITextViewTextDidBeginEditingNotification, object: nil)

         //文本框编辑结束时,触发
         NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidEndEditing", name: UITextViewTextDidEndEditingNotification, object: nil)

         //文本框内容改变时,触发
         NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", name: UITextViewTextDidChangeNotification, object: nil)

二:相应方法和代理方法的实现

     func textDidBeginEditing()
         {
             println("开始输入文本...")
     }

     func textDidEndEditing()
         {
             println("结束输入...")
     }

     func textDidChange()
         {
             println("正在输入...")
     }

     /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
         // Get the new view controller using segue.destinationViewController.
         // Pass the selected object to the new view controller.
     }
     */

     // MARK: - UITextFieldDelegate
     func textViewShouldBeginEditing(textView: UITextView) -> Bool
     {
         return true //如果返回false,文本视图不能编辑
     }

     func textViewShouldEndEditing(textView: UITextView) -> Bool
     {
         return true //如果返回false,表示编辑结束之后,文本视图不可再编辑
     }

     func textViewDidBeginEditing(textView: UITextView)
     {
         //文本视图开始编辑,这个时候我们可以处理一些事情
     }

     func textViewDidEndEditing(textView: UITextView)
     {
         //文本视图编辑结束,这个时候我们可以处理一些事情
     }

     func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool
     {
         //文本视图内容改变时,触发本方法,能得到改变的坐标和改变的内容

         //如果是回车符号,则textView释放第一响应值,返回false
         if (text ==  "\n") {
             textView.resignFirstResponder()
             return false;
         }
         return true
     }

     func textViewDidChange(textView: UITextView)
     {
         //文本视图改变后触发本代理方法
     }

     func textViewDidChangeSelection(textView: UITextView)
     {
         //文本视图 改变选择内容,触发本代理方法
     }

     //@availability(iOS, introduced=7.0)
     func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
     {
         //链接在文本中显示。当链接被点击的时候,会触发本代理方法

         return true
     }

     //@availability(iOS, introduced=7.0)
     func textView(textView: UITextView, shouldInteractWithTextAttachment textAttachment: NSTextAttachment, inRange characterRange: NSRange) -> Bool
     {
         //文本视图允许提供文本附件,文本附件点击时,会触发本代理方法
         return true
     }

     //按钮显示方法
     override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {

         //判断有没有选中文字,
         //如果选择,输出选择的文本

         if(action == "shareWXMenu:" && isSelect)//选择文本,并点击分享到微信菜单
         {
             return true;
         }
         else if(action == "shareWBMenu:" && isSelect)//选择文本,并点击分享到微博菜单
         {

             return true;
         }

         return false; //不显示系统的菜单,改成true对比一下效果
     }

     //分享到微信
     func shareWXMenu(sender: AnyObject?)
     {
          {
             println((textView.text as NSString).substringWithRange(textView.selectedRange))
         }

         println("这里实现 分享到微信 功能")
     }
     //分享到微博
     func shareWBMenu(sender: AnyObject?)
     {
         println("这里实现 分享到微博 功能")
     }