在UILabel SWIFT末尾添加Read More / Read Less

时间:2018-05-27 07:37:59

标签: uilabel swift4

嗨我需要一个UILabel,阅读更多,阅读更少但除了this one之外没有找到任何好的答案但问题是,我不知道这个答案是做什么的。正如我已经尝试过这样,但是得到了错误,IN THIS ANSWER什么是“@ IBOutlet weak var lblHeight:NSLayoutConstraint!”

我的代码

import UIKit

class ExpandedLabelViewController: UIViewController {

  @IBOutlet weak var myLabel: UILabel!
  @IBOutlet weak var lblHeight: NSLayoutConstraint!
  @IBOutlet weak var btn: UIButton!

  var isLabelAtMaxHeight = false
  override func viewDidLoad() 
  {
    super.viewDidLoad()
    myLabel.text = "bla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla"

  }

  @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: "bla bla bla vvvbla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla", width: view.bounds.width, font: myLabel.font)
    }
  }
  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()
    lbl.adjustsFontSizeToFitWidth = true
    return lbl.frame.size.height

  }
}

在我的代码中,我有2个“ myLabel ”一个是UILabel,第二个是NSLayoutConstraint。

我得到了这个例外。

2018-05-27 12:28:53.274735+0500 testingControllers[8035:240013] -[UILabel setConstant:]: unrecognized selector sent to instance 0x7fbd0f513e60
2018-05-27 12:28:53.291991+0500 testingControllers[8035:240013] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel setConstant:]: unrecognized selector sent to instance 0x7fbd0f513e60'

这里有人需要帮助。

由于

1 个答案:

答案 0 :(得分:1)

请试试这个: -

class FirstViewController: UIViewController, UIGestureRecognizerDelegate {

    @IBOutlet weak var myLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        myLabel.text = "bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla"
        myLabel.numberOfLines = 2
        let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.labelAction(gesture:)))
        myLabel.addGestureRecognizer(tap)
        myLabel.isUserInteractionEnabled = true
        tap.delegate = self
    }

    @objc func labelAction(gesture: UITapGestureRecognizer)
    {
        if myLabel.numberOfLines == 0 {
            myLabel.numberOfLines = 2
        } else {
            myLabel.numberOfLines = 0
        }
    }
}

如果您想使用按钮处理:

@IBAction func action(_ sender: Any) {
    if myLabel.numberOfLines == 0 {
        myLabel.numberOfLines = 2
    } else {
        myLabel.numberOfLines = 0
    }
}

此外,您需要为标签高度设置约束,如下面的屏幕截图: enter image description here

  

如果要在标签文本末尾显示越来越少的文本,请相应地在标签中添加更多文本和更少文本。