Questions tagged [sizetofit]

- sizeToFit Resizes and moves the receiver view so it just encloses its subviews.

Declaration in OBJECTIVE-C

- (void)sizeToFit

Declaration in SWIFT

func sizeToFit()

Discussion Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.

You should not override this method. If you want to change the default sizing information for your view, override the sizeThatFits: instead. That method performs any needed calculations and returns them to this method, which then makes the change.

Availability Available in iOS 2.0 and later.

95 questions
190
votes
23 answers

Resizing UITableView to fit content

I am creating an app which will have a question in a UILabel and a multiple choice answers displayed in UITableView, each row showing a multiple choice. Questions and answers will vary, so I need this UITableView to be dynamic in height. I would…
MiMo
  • 4,515
  • 3
  • 20
  • 23
24
votes
6 answers

Make font grow together with UILabel (resized by Auto Layout) - how to do it in Interface Builder?

In a simple iPhone app I display a letter tile (custom UIView with an image and 2 labels) by the following code in viewDidLoad: DraggedTile *tile = [[[NSBundle mainBundle] loadNibNamed:@"DraggedTile" …
Alexander Farber
  • 18,345
  • 68
  • 208
  • 375
23
votes
6 answers

UILabel sizeToFit method not working properly

I'm trying to show a long chunk of text inside a UILabel in one line. The UILabel is a subview of UIScrollView so I can scroll and see the entire UILabel. My problem is that the sizeToFit method only partialy works. textLabel.attributedText =…
YogevSitton
  • 9,578
  • 10
  • 55
  • 90
21
votes
4 answers

How can I add padding to the intrinsic content size of UILabel?

I'm using autolayout on iOS7 and I have a problem like this: I'm putting a UILabel onto a UIView and I'm arranging my autolayout constraints so that the label's centerX = parent view's centerX. I'm not giving any width constraint to the label. When…
aslisabanci
  • 8,360
  • 10
  • 56
  • 76
14
votes
4 answers

Weird thing occurs when using sizetofit of a UITextView in IOS7

When I test my app on IOS7 simulator. Sometimes I found it is weird when I using sizeToFit of a UITextView. The frame after sizeToFit seems right but the text can only show partly just like the photo below. (The gray area represents the UITextView…
echo
  • 965
  • 1
  • 10
  • 31
11
votes
2 answers

UILabel and numberOfLines and sizeToFit:

On iOS 5. I have a UILabel element that is originally placed in a nib. I want the x placement to stay fixed. I want it to take either 1 line, or 2 lines. If more than two lines, it should use the line break setting to show the ellipsis. I use…
chadbag
  • 1,768
  • 2
  • 18
  • 34
9
votes
2 answers

In iOS - How do I fit a UILabel to its text, without changing its position?

I'm calling sizeToFit on a UILabel which has right aligned text in it. It shrinks the height and width of the UILabeland fits the text to the top left of the UILabel. Now...the position of the UILabel is incorrect. How can I make the UILabel stay in…
Doron
  • 3,028
  • 6
  • 33
  • 56
9
votes
2 answers

Resize UItextview according to it's content in iOS7

I'm trying to resize a text view according to content & also it's sibling and parent container. Below code is working fine in iOS 6 if (/* less than ios 7 */) { CGRect frame = _textView.frame; CGSize conSize =…
Piyuesh
  • 1,006
  • 1
  • 9
  • 18
8
votes
1 answer

CATextLayer wrapped sizeToFit?

If I set textLayer.wrapped = YES, how do I resize textLayer to contain the wrapped text? I.e., how do I get the new height of the textLayer? Basically, I want something like -[UILabel sizeToFit].
ma11hew28
  • 106,283
  • 107
  • 420
  • 616
7
votes
1 answer

IOS8 - [UIlabel sizeToFit] causes EXC_BAD_ACCESS KERN_INVALID_ADDRESS crash

Our users are experiencing recurring crashes in the method below. The crashes happen always in the one of the lines: [lbl sizeToFit] (the lbl variable is re-initialized and reused. to my understanding, this should not cause any issues). These…
Matan Guttman
  • 598
  • 7
  • 13
7
votes
5 answers

UITableView with UILabel SizeToFit get mess when Scrolling

Hi everyone I have problem with my tableview, i make Cell with uilabel with SizeToFit and then calculate the UILabel Height for set the cell Height everything work well except when i scrolling my tableView the text get weird like one char per…
Dekel Maman
  • 2,007
  • 1
  • 13
  • 16
6
votes
2 answers

UILabel text to fill in the frame width

I want to build typography poster using UILabel left and margin: 25 320(device width) - 50(sum of the margins) = 270(label width frame) The font size of each label should change in order to fit in 270 frame width I try with…
user4460910
5
votes
2 answers

sizeToFit with maximum width

I am trying to programmatically manipulate a UILabel so that it has a dynamic width. I found that using sizeToFit() does this exact behavior. Now as this label becomes my navigationItem.titleView. I want it to have a maximum size, which can't be…
JVS
  • 1,994
  • 3
  • 16
  • 28
4
votes
1 answer

Correct way to use sizeThatFits: and sizeToFit with UILabel?

At the moment I have a label being sized correctly using [aString sizeWithFont:constrainedToSize:lineBreakMode:] but I've introduced rotation into my device and with a flexible width and height this results in my UILabel stretching width ways…
Thomas Clayson
  • 28,448
  • 25
  • 135
  • 216
4
votes
1 answer

Xcode - Swift - UILabel height expanding for text

I want to make a UILabel's height expand depending on its text. Here is what the view controller looks like, with the label selected: Here is the code (I have tried a bunch of different similar things but this is what I have right now): import…
Infamous911
  • 1,263
  • 1
  • 21
  • 34
1
2 3 4 5 6 7