2017-11-18 151 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。您只聲明瞭標籤高度限制。 –

相關問題