专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构

发微博界面添加textView

  • IOS中用户可以 输入文字 的控件
    • UITextField
      • 1.只能输入一行文本
      • 2.有占位文本
      • 3.继承UIControl不能滚动
    • UITextView
      • 1.可以输入多行文本
      • 2.没有占位文本
      • 3.继承UIScrollView可以滚动
  • 撰写微博界面的 textView
    • 1.显示多行
    • 2.可以滚动
    • 3.有占位文本
  • 系统没有提供这样的控件.使用 UITextView 再添加占位文本
  • HMComposeViewController.xib 拖入一个 UITextView,设置约束 填充父控件,并设置UITextView的属性
  • UITextView 关联到控制器
  • 自定义 UITextView 来实现 textView占位文本显示和隐藏

    • 新建 HMPlaceholderTextView

      class HMPlaceholderTextView: UITextView {
      
        // MARK: - 属性
        /// 占位文本
        var placeholder: String? {
            didSet {
                placeholderLabel.text = placeholder
      
                placeholderLabel.sizeToFit()
            }
        }
      
        // MARK: - 构造方法
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
      
            setupUI()
        }
      
        override init(frame: CGRect, textContainer: NSTextContainer?) {
      
            super.init(frame: frame, textContainer: textContainer)
      
            setupUI()
        }
      
        // MARK: - 准备UI
        /// 准备UI
        private func setupUI() {
            // 添加子控件
            addSubview(placeholderLabel)
      
            // 添加约束
            // 如果想给别的项目使用,最好少依赖第三方框架
            placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
      
            addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 8))
            addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 5))
        }
      
        // MARK: - 懒加载
        // 添加占位文本
        private lazy var placeholderLabel: UILabel = {
            // 创建label
            let label = UILabel()
      
            label.text = "占位文本"
      
            // 设置字体大小
            label.font = UIFont.systemFont(ofSize: 18)
      
            // 设置文字颜色
            label.textColor = UIColor.lightGray
      
            label.sizeToFit()
      
            return label
        }()
      }
      
    • HMComposeViewController.xib 里面的 UITextView 类型设置为 HMPlaceholderTextView
    • HMPlaceholderTextView 里面使用 delegate来监听文字改变

      override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
      
        prepareUI()
      
        // 使用代理监听textView文字改变
        delegate = self
      }
      
    • HMPlaceholderTextView 实现 UITextViewDelegate文字改变 方法: textViewDidChange
      // MARK: - 扩展 HMPlaceholderTextView 实现 UITextViewDelegate 协议
      extension HMPlaceholderTextView: UITextViewDelegate {
        func textViewDidChange(_ textView: UITextView) {
            // 当textView没有文字时显示占位文本
            // 有文字返回true
            placeholderLabel.isHidden = self.hasText
        }
      }
      
    • HMComposeViewController 里面使用delegate监听 textView文本改变,来设置导航栏右边的item状态
      // 添加代理,监听文字改变来设置导航栏右边按钮
      textView.delegate = self
      
    • HMComposeViewController 实现 UITextViewDelegate文字改变 方法: textViewDidChange
      // MARK: - 扩展 HMComposeViewController 实现 UITextViewDelegate代理
      extension HMComposeViewController: UITextViewDelegate {
        func textViewDidChange(_ textView: UITextView) {
            sendButton.isEnabled = textView.hasText
        }
      }
      
    • 发现导航栏的按钮状态正常,但是 textView 的占位文本显示不正常了.因为 HMPlaceholderTextView 的代理被 HMComposeViewController 的覆盖了
    • HMPlaceholderTextView 修改为使用 通知 来监听 文本改变

      required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
      
        setupUI()
      
        // 使用代理监听textView文字改变
      //        delegate = self
      
        // 使用代理监听textView文字改变,有问题
        //        delegate = self
        // 使用通知
        // object: 表示通知的发送者.
        // object = nil 表示任何人发送的 UITextViewTextDidChangeNotification 都能接受到
        // 指定 object, 表示只有 object 发送出来的 UITextViewTextDidChangeNotification 才能接受到
        NotificationCenter.default.addObserver(self, selector: #selector(textViewDidChange(_:)), name: NSNotification.Name.UITextViewTextDidChange, object: self)
      }
      
      func textViewDidChange(notification: NSNotification) {
        // textView没有文本的时候显示占位文本
        placeholderLabel.isHidden = self.hasText
      }
      
      deinit {
        // 注销通知
        NotificationCenter.default.removeObserver(self)
      }
      

results matching ""

    No results matching ""