Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do "wrap_content" (Android XML syntax) in iOS Swift

Tags:

ios

swift

I am an Android developer trying to learn iOS development in Swift. I was wondering how you achieve "wrap_content" in Xcode. Any ideas? What are the variables to change in the attribute inspector?

like image 380
andyjslee Avatar asked Aug 28 '15 04:08

andyjslee


3 Answers

The Android docs state that wrap_content:

tells your view to size itself to the dimensions required by its content.

This sounds analogous to what Apple calls the intrinsic content size. They detail how to configure it in the attribute inspector here.


To override the intrinsic content size of a UIView (such as a UILabel or a UIButton), you would simply have to override UIView's intrinsicContentSize() method.

like image 92
pxlshpr Avatar answered Nov 04 '22 12:11

pxlshpr


Try following code that fits textView's frame according to it's contained data-

let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
var newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
textView.frame = newFrame;
like image 28
NehaK Avatar answered Nov 04 '22 11:11

NehaK


u can do this in storyboard

  1. select the UIView*
  2. in Attribute Inspector select Line Breaks
  3. choose Word Wrap

be sure no other constraint defined for the view which limit the width!

like image 2
Navid Avatar answered Nov 04 '22 12:11

Navid