简体   繁体   中英

Swift 3 - Limit number of characters in uitextview

I am giving a text view to tweet some string .

I am applying the following method to restrict the number of characters to 140 in length.

code in a 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

     }

}

here is The textView class

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 {

    }
  }

}

// Any solution please

 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if txtTwitt.text.characters.count <= 140
        {
            // Code here
        }
        else
        {
            //Code Here
        }
        return true
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM