sizeToFit方法,是UIView的一个系统方法,苹果文档如下描述:
- (void)sizeToFit |
|
Description |
Resizes and moves the receiver view so it just encloses its subviews. |
|
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 |
iOS (2.0 and later) |
这个方法,会自动调整view的bounds,使用最小的、符合需求的区域大小。
猜想这个方法用来计算label的宽高会很方便,但是一开始未达到效果,代码如下
UIView *view1 = [[UIView alloc]init];
view1.frame =CGRectMake(100,100, 200, 200);
view1.backgroundColor = [UIColor grayColor];
[self.view addSubview:view1];
UILabel *label = [[UILabel alloc]init];
label.frame =CGRectMake(0,0,200, 200);
label.backgroundColor = [UIColor redColor];
[view1 addSubview:label];
label.text =@"1dsafafasfasfasfafasfdsafasdfsadfsafdsafasfdsf";
[label sizeToFit];
效果如下:
这个label为什么没有根据view1的宽度来适配呢?原来label的numberOfLines默认为1,此时sizeToFit不改变label的行数。
增加一行代码:
label.numberOfLines = 0;//设置为0,sizeToFit会自动调整行数
效果如下:
demo代码
- (void)viewDidLoad {
[super viewDidLoad];
//modify storyboard constraint, can not modify the constraints already setted in storyboard, even if can
//change the final position, will show constraints conflict error
self.testButtonTopConstraint.constant = 120.0f;
UILayoutGuide *viewLayoutGuide = self.view.layoutMarginsGuide;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"测试" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(moveTestButtonFrame:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
button.translatesAutoresizingMaskIntoConstraints = NO;
//use NSLayoutXAxisAnchor, NSLayoutYAxisAnchor, NSLayoutDimension
NSLayoutConstraint *buttonleft = [button.leadingAnchor constraintEqualToAnchor:viewLayoutGuide.leadingAnchor constant:40.0f];
NSLayoutConstraint *buttonWidth = [button.widthAnchor constraintEqualToConstant:60.0f];
NSLayoutConstraint *buttonHeight = [button.heightAnchor constraintEqualToConstant:60.0f];
NSLayoutConstraint *buttonTop = [button.topAnchor constraintEqualToAnchor:viewLayoutGuide.topAnchor constant:60.0f];
[self.view addConstraints:@[buttonleft, buttonWidth, buttonTop, buttonHeight]];
//equal to
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setTitle:@"测试2" forState:UIControlStateNormal];
button2.backgroundColor = [UIColor yellowColor];
[button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:button2];
button2.translatesAutoresizingMaskIntoConstraints = NO;
[[button2.leadingAnchor constraintEqualToAnchor:viewLayoutGuide.leadingAnchor constant:0.0f] setActive:YES];
[[button2.topAnchor constraintEqualToAnchor:viewLayoutGuide.topAnchor] setActive:YES];
[[button2.widthAnchor constraintEqualToConstant:80] setActive:YES];
[[button2.heightAnchor constraintEqualToConstant:44.0f] setActive:YES];
//check less than , greater than,
UILabel *label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentLeft;
label.numberOfLines = 2;
label.textColor = [UIColor redColor];
label.text = @"label constraints ";
label.backgroundColor = [UIColor blueColor];
[label sizeToFit];
[self.view addSubview:label];
//CGSize lableSize = [label sizeThatFits:CGSizeMake(120.0f, 0.0f)];
label.translatesAutoresizingMaskIntoConstraints = NO;
[[label.topAnchor constraintEqualToAnchor:viewLayoutGuide.topAnchor constant:0.0f] setActive:YES];
[[label.leadingAnchor constraintEqualToAnchor:viewLayoutGuide.leadingAnchor constant:0.0f] setActive:YES];
[[label.widthAnchor constraintEqualToConstant:199] setActive:YES];
[[label.heightAnchor constraintLessThanOrEqualToConstant:80.0f] setActive:YES];
//test sizeThatFits method if can get the right height
UILabel *label2 = [[UILabel alloc] init];
label2.textAlignment = NSTextAlignmentLeft;
label2.textColor = [UIColor blueColor];
label2.lineBreakMode = NSLineBreakByWordWrapping;
label2.backgroundColor = [UIColor yellowColor];
label2.numberOfLines = 0;
label2.text = @"123sljfldsjfldsjfdlksjfdslkfjlcefhkfsjfsdjfldjfldsjfldjfdlsjflsdjfldsjfsdljfdlsfjl789";
[self.view addSubview:label2];
//sizeThatFits althought set orignal width, but the final result width is not same as it --- the lable height is 203.000000 and width is 294.500000
CGSize finalSize = [label2 sizeThatFits:CGSizeMake(300.0f, 0.0f)];
NSLog(@"--- the lable height is %f and width is %f", finalSize.height, finalSize.width);
label2.translatesAutoresizingMaskIntoConstraints = NO;
[[label2.topAnchor constraintEqualToAnchor:viewLayoutGuide.topAnchor constant:300.0f] setActive:YES];
[[label2.leadingAnchor constraintEqualToAnchor:viewLayoutGuide.leadingAnchor constant:0.0f] setActive:YES];
[[label2.widthAnchor constraintEqualToConstant:finalSize.width] setActive:YES];
[[label2.heightAnchor constraintEqualToConstant:finalSize.height] setActive:YES];
//the width and height is same as setting before
[label2 layoutIfNeeded];
NSLog(@"--- the label2 orignal x is %f and orignal y is %f and width is %f and height is %f", label2.bounds.origin.x, label2.bounds.origin.y, label2.frame.size.width, label2.frame.size.height);
//sizetofit show complete
UILabel *label3 = [[UILabel alloc] init];
label3.textAlignment = NSTextAlignmentLeft;
label3.textColor = [UIColor greenColor];
label3.lineBreakMode = NSLineBreakByWordWrapping;
label3.backgroundColor = [UIColor redColor];
label3.text = @"label 3 测试数据减肥了大家来上课加夫里什的加夫里什克己复礼开始减肥了多款是加夫里什大家发来开始减肥了可是封疆大吏是克己复礼的时刻减肥时间法兰克福觉得失联飞机";
[label3 sizeToFit];
[self.view addSubview:label3];
label3.numberOfLines = 0;
CGSize label3Szie = [label3 sizeThatFits:CGSizeMake(100.0f, 0.0f)];
label3.translatesAutoresizingMaskIntoConstraints = NO;
[[label3.topAnchor constraintEqualToAnchor:viewLayoutGuide.topAnchor constant:150.0f] setActive:YES];
[[label3.leadingAnchor constraintEqualToAnchor:viewLayoutGuide.leadingAnchor constant:0.0f] setActive:YES];
[[label3.widthAnchor constraintEqualToConstant:100.0f] setActive:YES];
//constraintLessThanOrEqualToConstant will change the label bounds, if only one row, will always show on the top screen
[[label3.heightAnchor constraintLessThanOrEqualToConstant:label3Szie.height] setActive:YES];
[label3 layoutIfNeeded];
//the sizetofit height and siztthatfits heigh is equal, so only need use sizetofit will simple get the final height for all the words
NSLog(@"---- the label3 height is %f and size that fit heigh is %f", label3.frame.size.height, label3Szie.height);
}