简体   繁体   中英

How to display just few from original text using UITextView - swift

I am creating a sample app to display some texts which contain many lines of content using UITextView . For example, my texts has 400 characters then it should display only 200 characters and end with ... for identify to user that content has more text to display, then i just put some button which title "more detail", by click on that button then 400 characters should be shown.

How can i achieve something like that? Thank in advance.

One of the textView methods might be useful for your purpose. With the textView delegate method given below, you can limit textView characters to 200. Check the code below for limiting the text to 200 characters.

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
    return newText.count <= 200
    }

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