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
0
votes
0 answers

UILabel and UIButton sizeToFit height are different in Swift

I am trying to align my UIButton and UILabel, however when I use size to fit for each of them the frame is of a different height. I want my UIButton frame height to be the same height as my UILabel. Both views added as subview on my main view, in my…
bain
  • 223
  • 3
  • 14
0
votes
1 answer

SizeToFit() doesn't work properly with \n?

I create a label programmatically. It will always have two lines. However, the font size of these two lines will change. Therefore, the height and width of the label will have to change. I try to do this using sizeToFit. However, the second word…
Everett
  • 307
  • 3
  • 14
0
votes
2 answers

UILabel SizetoFit After Specifying Number of Lines?

I would like to create a UILabel programmatically after specifying the number of lines. I am using Swift. I have the following code: let newLabel = UILabel() newLabel.text = "Hello There" newLabel.font = UIFont.systemFont(ofSize:…
Everett
  • 307
  • 3
  • 14
0
votes
1 answer

iOS UITextView sizeToFit make height doubled of content text in iPad

Below is my code of textView, but sizeToFit makes textView's height more than doubled of it's content text. Please provide any solution UITextView *paragraphView = [[UITextView alloc] initWithFrame: CGRectMake(10, 10, 295, 30)]; [paragraphView…
Shiv Pogra
  • 73
  • 5
0
votes
1 answer

why is the "sizeToFit" call required in this code?

Anyone able to highlight why the "[aLabel sizeToFit]" line is required in this code below, taken from here. That is, the CGRect created has already been sized based on the text font size, so why would the sizeToFit be required? Or is it because…
Greg
  • 31,898
  • 75
  • 232
  • 424
0
votes
1 answer

UILabel sizeToFit crashed when OS version is 11.2 and device's language is Japanese

I have a simple code as the follow UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)]; label.text = @"アクアレア カベルネソーヴィニヨン"; label.font = [UIFont systemFontOfSize:11.0f]; label.numberOfLines = 0; [label sizeToFit]; When I set…
0
votes
2 answers

How to programmatically sizeToFit width AND height on UILabel?

I'm programmatically creating multi-line UILabels ([label setNumberOfLines:0];). The built-in sizeToFit method of UILabel works great for 1 line UILabels, but for multi-line text, it sets the height properly, but the width is set too small, causing…
ByteSlinger
  • 1,221
  • 12
  • 23
0
votes
1 answer

Adjusting the width of a UIButton depending on its title

I have a lot of trouble solving this. I have the following situation. In my storyboard I have a table view and in the table view cell I have a UIButton and another UIButton next to it. The first button has text and the second button has a star…
symenize
  • 35
  • 4
0
votes
1 answer

Use SizeToFit() on a UILabel with a shadow?

I am having some UILabel elements for which a dynamically alter the text. I also add shadows to the labels using label.Layer.ShadowOffset and dito color. When the text of a label has been changed I use SizeToFit. Problem: That method does not take…
Ole
  • 21
  • 1
0
votes
0 answers

sizeToFit not working like we expecting

I face a strange problem with an UILabel in UITableView. I have a label inside of a cell and I want use sizeToFit for it because I will move a UIImageView right after text. The problem is that the first time sizeToFit does not work as expected but…
Gaby Fitcal
  • 1,624
  • 1
  • 15
  • 25
0
votes
2 answers

UIButton sizeToFit not working

This is my code : let button_video = UIButton(type: .custom) button_video.frame = CGRect.zero if subsession.VideoUrl != nil { button_video.frame = CGRect(x: 10, y: yAxis + 15, width: 300, height: 30) button_video.contentEdgeInsets =…
Nitish
  • 13,002
  • 23
  • 121
  • 243
0
votes
1 answer

UILabel sizeToFit doesn't stretch to width

I am programmatically creating a UILabel like this: hashtagLbl = UILabel(frame: CGRectMake(20, nameLbl.frame.maxY + 5, bounds.width-40, layerView.frame.width/3.66)) hashtagLbl.text = "#Placeholder" hashtagLbl.font =…
JVS
  • 1,994
  • 3
  • 16
  • 28
0
votes
1 answer

UIWebView sizeToFit() Smaller

It appears that using sizeToFit() on a UIWebView does not change the size to be smaller, only larger. This is some set-up code: var scrollView: UIScrollView! var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() // Do…
Doug Mead
  • 716
  • 5
  • 16
0
votes
1 answer

UILabel not displaying everything

I'm seriously lost on this one. Here's the shot: The price on Tea's Tea Matcha... is being cut off. I simply cannot figure out why. I'm using boundingRectWithSize:... When I log the height of the label's frame after calling layoutIfNeeded, it's the…
Tony Friz
  • 791
  • 9
  • 22
0
votes
2 answers

Text not fitting the width of container

This looks like it would be a simple problem but I can't get my head around it. As you can see here, the text uses the width of the container, but when I change the width of the container, the text fits but it looks like this and pushes the sidebar…
hadesg
  • 17
  • 5