0

I need to add read more at the end of the 3rd line in label.

like this i need:

enter image description here

Note: i have tried this answer Add "...Read More" to the end of UILabel but here read more action is not working and expand collapse also not working.. so tried like below

Actually i need to show description in label.. here if the description if more then 3 lines then i need to show read more at end of the 3rd line and need to expand the label with remaining text..

code: with this code i can expand the label if more than 3 lines but i need to show read more at end of the third line

override func viewDidLoad() {
    super.viewDidLoad()

 despLbl.text = publicProGenInfo?.result?.user?.description
        despLbl.numberOfLines = 3//despLbl.maxNumberOfLines//3
        let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.labelAction(gesture:)))
        despLbl.addGestureRecognizer(tap)
        despLbl.isUserInteractionEnabled = true
        tap.delegate = self

   }

 @objc func labelAction(gesture: UITapGestureRecognizer)
  {
      if despLbl.numberOfLines == 3 {
          despLbl.numberOfLines = despLbl.maxNumberOfLines
      } else {
          despLbl.numberOfLines = 3
      }
  }

o/p: getting like this.. here after 3rd line how to add read more text... please do guide

enter image description here enter image description here

Json Swift
  • 31
  • 1
  • 7

1 Answers1

0

ReadMoreTextView is a good option for this purpose, with a few lines of code you can achieve this behavior.

@IBOutlet weak var aboutMessageTextView: ReadMoreTextView!
aboutMessageTextView.text = "your text"
let readMoreTextAttributes: [NSAttributedString.Key: Any] = [
     NSAttributedString.Key.foregroundColor: UIColor.systemBlue,
     NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14)
   ]
aboutMessageTextView.attributedReadMoreText = NSAttributedString(string: " ... Read more", attributes: readMoreTextAttributes)
aboutMessageTextView.maximumNumberOfLines = 3
aboutMessageTextView.shouldTrim = true

khawar ali
  • 263
  • 1
  • 7