如何添加“阅读更多”在Swift的Uilabel的第三行结束时

发布于 2025-01-30 18:36:44 字数 1321 浏览 4 评论 0原文

我需要在标签第三行的末尾添加更多。

这样我需要:

​/添加读取的uilabel“> add” ...在uilabel的末尾读取更多“ ...因此,我需要像下面的尝试一样尝试

。在此处,如果说明更多,则我需要在第三行的结尾显示剩下的文本..

如果超过3行,我可以扩展标签

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
      }
  }

代码:使用此代码, :得到这样的..在第三行之后,如何添加阅读更多文本...请执行指南

“在此处输入图像描述”

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 hereenter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

峩卟喜欢 2025-02-06 18:36:44

readmoretextview 是一个很好的选择,有几行代码,您可以实现此行为。

@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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文