Read More/Less with Swift 3

前端 未结 2 543
迷失自我
迷失自我 2020-12-10 08:15

I want to add \"Read more\" at the end of the paragraph. When I click on the \"Read more\" text, it should be expand and display \"Less\" a

相关标签:
2条回答
  • 2020-12-10 09:06

    I have done it with trimming the string.

    we can compare the string characters length by .count and we can hide read more button if there are less only very few characters in the string.

    And removed the last word after trimming to ensure that no visible words are cut off. Then added "...." at the end

            var trimData = ""
    
            if eventData.eventDescription.count > 500 {
                cell.readMoreLabel.isHidden = false
    
                if !readMore {
    
                    if eventData.eventDescription.count > 500 {
    
                        trimData = String(eventData.eventDescription.prefix(500))
    
                        trimData = trimData.components(separatedBy: " ").dropLast().joined(separator: " ")
    
                        trimData = trimData+"...."
                    } else {
                        trimData = eventData.eventDescription
                    }
    
                    cell.readMoreLabel.attributedText = NSAttributedString(string: "Read More", attributes:
                        [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
    
    
    
                } else {
                    trimData = eventData.eventDescription
                    cell.readMoreLabel.attributedText = NSAttributedString(string: "Read Less", attributes:
                        [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
                }
    
            } else {
                trimData = eventData.eventDescription
                 cell.readMoreLabel.isHidden = true
            }
    
    0 讨论(0)
  • 2020-12-10 09:08
    • Create an outlet for height constraint of your messageLabel
    • Set top layout of your "Read more" button to messageLabel
    • On clicking "Read more" button increase height constraint constant, on clicking "Read less" decrease height constraint constant.

      @IBOutlet weak var btn: UIButton!
      
      @IBOutlet weak var lblHeight: NSLayoutConstraint!
      
      var isLabelAtMaxHeight = false
      
      @IBAction func btnAction(_ sender: Any) {
          if isLabelAtMaxHeight {
              btn.setTitle("Read more", for: .normal)
              isLabelAtMaxHeight = false
              lblHeight.constant = 70
          }
          else {
              btn.setTitle("Read less", for: .normal)
              isLabelAtMaxHeight = true
              lblHeight.constant = getLabelHeight(text: yourSummaryText, width: view.bounds.width, font: yourSummaryLabel.font)
          }
      }
      

    Get height of a text

        func getLabelHeight(text: String, width: CGFloat, font: UIFont) -> CGFloat {
            let lbl = UILabel(frame: .zero)
            lbl.frame.size.width = width
            lbl.font = font
            lbl.numberOfLines = 0
            lbl.text = text
            lbl.sizeToFit()
    
            return lbl.frame.size.height
        }
    
    0 讨论(0)
提交回复
热议问题