2016-09-02 5 views
0

UItextviewの高さを調整する方法を見つけようとしていますが、回答が見つかりませんでした。だから、私はあなたの助けを求めています。私はストーリーボードを使って制約を作りません。コンテンツサイズに基づいてUITextViewの高さをどのように高めますか?ご協力ありがとうございました!私は、テキストビューの様子を示す画像を添付しました。iOS 9 Swift、コンテンツに基づくUITextViewの高さ


enter image description here

scrollView.addSubview(itemTitleView) 
     itemTitleView.topAnchor.constraintEqualToAnchor(itemImage.bottomAnchor, constant: 80).active = true 
     itemTitleView.widthAnchor.constraintEqualToAnchor(itemImage.widthAnchor).active = true 
     itemTitleView.heightAnchor.constraintEqualToConstant(56).active = true 
     itemTitleView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true 
+0

ラベルであれば簡単でしょう。それは本質的な高さと幅を持っているからです。編集できない場合は、textViewではなくUILabelを使用できます。 –

+0

はあなたのテキストビューを編集できますか?または単に表示目的のために? – jo3birdtalk

答えて

0

文字列は、この拡張機能を使用してみてください。これは幅がどれくらいであるかを知っていれば必要な身長を与えます。幅とフォントだけが必要で、うまくいくはずです。

extension String { 
    func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat { 
     let constraintRect = CGSize(width: width, height: CGFloat.max) 

     let boundingBox = self.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) 

     return boundingBox.height 
    } 
} 
関連する問題