0

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

2
  • Have you set the ViewController as your textView delegate ? txtDescription.delegate = self Commented Jul 8, 2017 at 16:16
  • yes , I set delegate Commented Jul 8, 2017 at 16:24

1 Answer 1

3
 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if txtTwitt.text.characters.count <= 140 { // Code here } else { //Code Here } return true } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.