あなたはこの事がこのように作業することができます:
1.ストーリーボードでUITextViewとUIButtonを追加します。

2.自動レイアウトの制約を使用している場合は、UITextViewの高さの出口を作ります。 ViewControllerクラスで

3としてアウトレットを作る:
@IBOutletするvar txtView:UITextView!
@IBOutlet var btn_Show:UIButton!
@IBOutlet var textView_HeightConstraint:NSLayoutConstraint!
4. UITextViewのテキストの高さを示す方法を作成します。ボタンのクリック時
func getRowHeightFromText(strText : String!) -> CGFloat
{
let textView : UITextView! = UITextView(frame: CGRect(x: self.txtView.frame.origin.x,
y: 0,
width: self.txtView.frame.size.width,
height: 0))
textView.text = strText
textView.font = UIFont(name: "Fira Sans", size: 16.0)
textView.sizeToFit()
var txt_frame : CGRect! = CGRect()
txt_frame = textView.frame
var size : CGSize! = CGSize()
size = txt_frame.size
size.height = 50 + txt_frame.size.height
return size.height
}
5:
@IBAction func showMore_Button_Clicked(_ sender: UIButton)
{
if sender.tag == 0
{
let height = self.getRowHeightFromText(strText: self.txtView.text)
self.textView_HeightConstraint.constant = height
self.view.layoutIfNeeded()
btn_Show.setTitle("ShowLess", for: .normal)
sender.tag = 1
}
else
{
self.textView_HeightConstraint.constant = 116
self.view.layoutIfNeeded()
btn_Show.setTitle("ShowMore", for: .normal)
sender.tag = 0
}
}
6.あなただけのフレーム(高さ)を変更し、AutoLayoutConstraintsを使用していない場合UIButtonのクリックによるUITextView。 「textView_HeightConstraint
」の必要はありません
@IBAction func showMore_Button_Clicked(_ sender: UIButton)
{
if sender.tag == 0
{
let height = self.getRowHeightFromText(strText: self.txtView.text)
self.txtView.frame = CGRect(x: self.txtView.frame.origin.x, y: self.txtView.frame.origin.y, width: self.txtView.frame.size.width, height: height)
btn_Show.setTitle("ShowLess", for: .normal)
sender.tag = 1
}
else
{
self.txtView.frame = CGRect(x: self.txtView.frame.origin.x, y: self.txtView.frame.origin.y, width: self.txtView.frame.size.width, height: 116)
btn_Show.setTitle("ShowMore", for: .normal)
sender.tag = 0
}
}
実装が非常に簡単です。 以下の手順に従います。 1.静的な高さのUITextViewを作成し、textviewの下部に「Showmore」ボタンを追加します。 2. uibxtをクリックすると、uitextviewの高さが変更されます。その中のテキストに従って高さを変更し、ボタンのフレームも更新します。 「Butto」のタイトルを「Showless」に変更します。 – SNarula