Swift 3 - 限制 uitextview 中的字符数

Posted

技术标签:

【中文标题】Swift 3 - 限制 uitextview 中的字符数【英文标题】:Swift 3 - Limit number of characters in uitextview 【发布时间】:2017-07-08 08:59:30 【问题描述】:

我正在提供一个文本视图来推特一些字符串。

我正在应用以下方法将字符数限制为 140 个长度。

viewController 中的代码

import UIKit
import SVProgressHUD
import Toaster

class ViewController: UIViewController , UITextViewDelegate , 
UITextFieldDelegate 


var checktoaster = false
@IBOutlet weak var txtDescription: UITextView!



func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool 
    if txtDescription == textView
    
        guard let tex = textView.text else  return true 
        let newLength = tex.characters.count + text.characters.count - range.length
        if !(newLength <= 140)
        
            if checktoaster == false
            
                checktoaster = true
                let toast = Toast(text: "You have write only 140 character.", duration: Delay.long)
                toast.show()
            
        
        else
        
            checktoaster = false
        
        return newLength <= 140 //Bool
     
    return true

     


这里是 textView 类

import UIKit
public class LetsTextView : UITextView , UITextViewDelegate

required public init?(coder aDecoder: NSCoder) 
    super.init(coder: aDecoder)
    NotificationCenter.default.addObserver(self, selector: #selector(LetsTextView.refreshPlaceholder), name: NSNotification.Name.UITextViewTextDidChange, object: self)


override init(frame: CGRect, textContainer: NSTextContainer?) 
    super.init(frame: frame, textContainer: textContainer)
    NotificationCenter.default.addObserver(self, selector: #selector(LetsTextView.refreshPlaceholder), name: NSNotification.Name.UITextViewTextDidChange, object: self)


override public func awakeFromNib() 
    super.awakeFromNib()
    NotificationCenter.default.addObserver(self, selector: #selector(LetsTextView.refreshPlaceholder), name: NSNotification.Name.UITextViewTextDidChange, object: self)


deinit 
    NotificationCenter.default.removeObserver(self)



@IBInspectable  public var placeholder : String? 

    get 

        return placeholderLabel?.text
    

    set 

        if placeholderLabel == nil 
            var frm = self.bounds.insetBy(dx: 5, dy: 6)
            frm.size.height = 20
            placeholderLabel = UILabel(frame:frm)

            if let unwrappedPlaceholderLabel = placeholderLabel 

                unwrappedPlaceholderLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
                unwrappedPlaceholderLabel.lineBreakMode = .byWordWrapping
                unwrappedPlaceholderLabel.numberOfLines = 0
                unwrappedPlaceholderLabel.font = self.font
                unwrappedPlaceholderLabel.backgroundColor = UIColor.clear
                unwrappedPlaceholderLabel.textColor = UIColor.gray//UIColor(red: 255/255, green: 190/255, blue: 70/255, alpha: 1.0)
                unwrappedPlaceholderLabel.alpha = 0
                addSubview(unwrappedPlaceholderLabel)
            
        

        placeholderLabel?.text = newValue
        refreshPlaceholder()
    


func refreshPlaceholder() 

    if text.characters.count != 0 
        placeholderLabel?.alpha = 0
     else 
        placeholderLabel?.alpha = 1
    



override public var text: String! 

    didSet 

        refreshPlaceholder()

    


override public var font : UIFont? 

    didSet 

        if let unwrappedFont = font 
            placeholderLabel?.font = unwrappedFont
         else 
            placeholderLabel?.font = UIFont.systemFont(ofSize: 12)
        
    


override public var delegate : UITextViewDelegate? 

    get 
        refreshPlaceholder()
        return super.delegate
    

    set 

    
  


// 请问有什么解决办法

【问题讨论】:

您是否将 ViewController 设置为您的 textView 委托? txtDescription.delegate = self 是的,我设置了委托 【参考方案1】:
 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool 
        if txtTwitt.text.characters.count <= 140
        
            // Code here
        
        else
        
            //Code Here
        
        return true
    

【讨论】:

以上是关于Swift 3 - 限制 uitextview 中的字符数的主要内容,如果未能解决你的问题,请参考以下文章

如何防止用户在 Swift 5 的 UITextView 中添加太多换行符?

Swift 3:UITextView - 动态高度 - 以编程方式

Swift 3在UITextView中更改所选文本的颜色

OC & Swift中UITextFiledUITextView限制输入字数

如何在 Swift 3 中将 NSConstraints 添加到 UITextView

在 Swift 中更改 UITextView 的 textColor