Correct way to use sizeThatFits: and sizeToFit with UILabel?

At the moment I have a label whose size is correct using [aString sizeWithFont: constrainedToSize: lineBreakMode:], but I entered rotation into my device and with flexible width and height, this leads to an increase in the width of the UILabel (since it is set relative to the main view), but then does not shrink in height to compensate for the extra space.

In another question, I asked what I was told to use sizeToFitand / or sizeThatFits:, however, I can not find any useful resource on the Internet that will tell me how to use it properly and the tests that I have done, resulting in irrational behavior.

In a nutshell, let's say I have this structure:

UIView *innerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 0)];
[innerView setAutoresizingMask:*flexible width and height*]
[innerView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:innerView];

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, innerView.frame.size.width-20, 0)];
[myLabel setAutoresizingMask:*flexible width and height*]
[myLabel setBackgroundColor:[UIColor blueColor]];
[myLabel setNumberOfLines:0];
[myLabel setLineBreakMode:UILineBreakModeWordWrap];
[innerView addSubview:myLabel];

( , , , , ... , ).

. , . , . [aString sizeWithFont...] . , sizeToFit sizeThatFits ?

.

Tom

+3
1

? interfaceOrientation :

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

didRotateFromInterfaceOrientation.

: http://cocoamatic.blogspot.com/2010/08/uilabel-dynamic-sizing-based-on-string.html

+2

Source: https://habr.com/ru/post/1771463/


All Articles