Peter Pik
Peter Pik

Reputation: 11193

set textView height to equal contentSize swift

I've added a textView to my tableViewCell through the interfacebuilder. However i want to set the size equal the content in the textView. this does not seem to work. So far i've done below, which does not change the frame of the textView

var size: CGSize = cell.titleViewLabel.systemLayoutSizeFittingSize(cell.titleViewLabel.contentSize)
var frame: CGRect = cell.titleViewLabel.frame
frame.size.height = size.height
cell.titleViewLabel.frame = frame

Upvotes: 0

Views: 3146

Answers (1)

tskulbru
tskulbru

Reputation: 2346

I'd recommend using autolayout instead of setting the frame size manually. However if you must set it manually you need to fetch the size of the text you are setting.

let labelSize = myLabel.text.sizeWithAttributes([NSFontAttributeName:myLabel.font!])

let labelHeight = labelSize.height;

Using this you should have the size and height of your text, which you then can set as the defined frame height.

Upvotes: 1

Related Questions