RXswift Setting maximum number of characters of UITextView

 Problem:

I want to set maximum number of characters for UITextview.How do i achieve that using rxswift?


Solution:

//    MARK: - Limit textview charecters

func limitTextViewCharacters(tv:UITextView?, limitInt) {

    if let tView = tv {

        guard tView.text.count > limit else {

            return

        }

        

        let index = tView.text.index(tView.text.startIndexoffsetBy: limit)

        tView.text = String(tView.text.prefix(upTo: index))

    }

}


Add the above function in utils.

Then in your viewcontroller add the below code in viewdidload


        TxtView.rx.text.changed

            .subscribe(onNext: {_ in

                limitTextViewCharacters(tvself.TxtViewlimitTextLimit)

                let name = self.TxtView.text

            }).disposed(byself.disposeBag)


Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift