2017-11-18 115 views
-1

我想在最后创建带有Read More/Read Less的段落(标签中)。我是iPhone应用程序开发的初学者。如何创建带有Read More/Read Less的段落(标签中)?

这是我的代码... 我得到在其他条件一个错误,错误是“类型NSLayoutConstraint价值”没有成员“字体””在lbl.contant =的getHeight ....

@IBOutlet weak var lbl: NSLayoutConstraint! 

@IBOutlet weak var btn: UIButton!   

var isLabelAtMaxHeight = false 

@IBAction func ButtonAction(_ sender: Any) { 
    if isLabelAtMaxHeight { 
     btn.setTitle("Read more", for: .normal) 
     isLabelAtMaxHeight = false 
     lbl.constant = 70 
    } 
    else { 
     btn.setTitle("Read less", for: .normal) 
     isLabelAtMaxHeight = true 
     lbl.constant = getHeightOfLabel(text:"Isn't it? How to do? I'm sorry I don't know how to solve this exactly. :(– May Phyu Feb 20 at 11:16 No, i dont mean like that. When you set text to label then U have to find height of label then assign that height to label with setting property number of lines = 0 – Jitendra Modi Feb 20 at 11:31" , width: 
      view.bounds.width, font: lbl.font) 
    } 
} 



func getHeightOfLabel(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

@IBOutlet weak var btn: UIButton! @IBOutlet weak var yourParagraphLabel: UILabel! @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 = getHeightOfLabel(text: yourParagraphText, width: view.bounds.width, font: yourParagraphLabel.font) } } 

获取高度这是过于宽泛,unresearched的堆栈溢出的问题,很遗憾。你会编辑它来显示你已经尝试过什么,以及你有什么具体问题? – halfer

+0

@ Shaik.Sahilpasha字体:lbl.font是你的错误。在这里给你的标签名称 –

+0

lbl是标签名称...... –

回答

0
  • 为您paragraphLabel的高度约束的出口。你的 “更多” 按钮paragraphLabel。
  • 机顶布局0

    点击“Read more”按钮增加高度约束常数,点击“Read less”降低高度约束常数。文本

    func getHeightOfLabel(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

一旦检查出我的代码我在ButtonAction其他条件得到一个错误.... –

+0

你能解释更多关于你的代码吗?哪一个是你的段落标签IBoutlet和哪一个你已经采用heightConstraint IBoutlet –

+0

我想你还没有申报IBOutlet UILabel。您只声明了标签高度限制。 –

相关问题