0

hopefully someone can guide me and help me out here!!

I'm retrieving some characters data from an API and I have stored the value in an IBOutlet type of UITextView named UITextDisplay. All I'm trying to do is display 300 characters from that data or either less, but for some reason isn't letting me.

Thank you in advance!!

I also delegate the UITextField to the ViewController so it should work and my code compile perfectly fine. The

extension ViewController: UITextViewDelegate
{
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
    {
        textView.text = UITextDisplay.text

        let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)

        return newText.count < 300

    }
}

Also.. is there any way to remove from the characters the end of it "+2637 characters"?? enter image description here

Look forward to any resolution! Amaury,

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
Amaury C.
  • 51
  • 1
  • 7
  • So you want to set a limit of MAX 300 characters in the textView? – Abhishek Patel Dec 18 '19 at 05:05
  • Does this answer your question? [Set the maximum character length of a UITextField](https://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield) (In my answer you can also see for UITextView) – Hitesh Surani Dec 18 '19 at 05:09
  • @Hitesh Tried.,. but it didn't worked! At this point I'm just figuring out if I'm calling it properly... :/ – Amaury C. Dec 18 '19 at 05:40
  • Exactly..@AbhishekPatel – Amaury C. Dec 18 '19 at 05:45
  • @AmauryC. To use this extension, You need to remove delegate eqaul to self(Your current UIViewController). That's it – Hitesh Surani Dec 18 '19 at 05:56
  • @Hitesh Thank you for your prompt response! So you want to remove the UITextView Delegate itself from the current ViewController?? You mentioned UIViewController.. but I created an extension from the ViewController, not the UIViewController. I'm a bit confused right now. Could you please elaborate your answer for me please? Thank you so much in advance. – Amaury C. Dec 19 '19 at 00:18

1 Answers1

1

Please have a look of below question

Set the maximum character length of a UITextField (In my answer you can also see for UITextView)

Instead of writing duplicate code everywhere, You can create an extension of UITextView or UITextField to set the max length

Update:

Comment Explanation:

First thing, You need to remove the UITextView delegate method from your current view controller. Because extension of UITextView is internally used UITextViewDelegate

Note: Delegate is one to one communication. In your case, if you implement delegate in the current view controller then an extension of UITextView is not worked.

Community
  • 1
  • 1
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65