Skip to content

LicaGao/SwiftLimitDemo

Repository files navigation

SwiftLimitDemo

11月22日练习

  • 模仿发微博界面,实现发微博时字数限制为140字的功能,超出后右下角字数统计label会变为橘色。
  • 使用通知来实现字数限制功能。在viewDidLoad中添加通知:
NotificationCenter.default.addObserver(self, selector: #selector(textViewNotificationAction(notification:)), name: NSNotification.Name.UITextViewTextDidChange, object: nil)

该通知调用方法 textViewNotificationAction :

    @objc func textViewNotificationAction(notification: Notification) {
        let limit: Int = 140
        let text = self.textView.text as NSString
        if text.length >= limit {
            let str = text.substring(to: limit)
            self.textView.text = str
            self.limitLabel.text = "\(limit)"
            self.limitLabel.textColor = UIColor.orange
        } else {
            self.limitLabel.textColor = UIColor.darkGray
            self.limitLabel.text = "\(text.length)"
        }
        self.weiboDetail = String(text)
    }

About

Swift 模仿微博发送界面 实现字数限制功能

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages