我想在最後創建帶有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
}
的
獲取高度這是過於寬泛,unresearched的堆棧溢出的問題,很遺憾。你會編輯它來顯示你已經嘗試過什麼,以及你有什麼具體問題? – halfer
@ Shaik.Sahilpasha字體:lbl.font是你的錯誤。在這裏給你的標籤名稱 –
lbl是標籤名稱...... –