0

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
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
  • Are you using auto layout? Why not just set some positioning constraints and let the intrinsic content size take over. – Andrew Robinson Apr 07 '15 at 16:25
  • possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – Dániel Nagy Apr 07 '15 at 17:53

1 Answers1

1

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.

tskulbru
  • 2,336
  • 1
  • 20
  • 38
  • AutoLayout. Could u explain or send me a link which could help me achieve what i want using that? – Peter Pik Apr 07 '15 at 16:36
  • Not a direct answer, but i recommend reading this post on how to use both autolayouts intrinsicContentSize as well as systemLayoutSizeFittingSize: http://stackoverflow.com/questions/18118021/how-to-resize-superview-to-fit-all-subviews-with-autolayout/18155803#18155803 – tskulbru Apr 07 '15 at 16:43