The difference between sizeToFit
官方解释:
//return'best'size to fit given size. doesnotactually resize view. Defaultisreturnexisting view size- (CGSize)sizeThatFits:(CGSize)size; // calls sizeThatFits:withcurrent view boundsandchanges bounds size.- (void)sizeToFit;
sizeToFit:计算出最优的size,然后再改变自己的size
sizeThatFits:同样是计算最优的size,但是不会改变自己的size
sizeToFit 的例子
UILabel*label = [[UILabelalloc] initWithFrame:CGRectMake(10,10,0,0)];[label setBackgroundColor:[UIColorgrayColor]];[label setFont:[UIFontsystemFontOfSize:20]];label.text =@"没有什么工作是加工资解决不了的,如果有,那就表示工资没到位";//sizeToFit:直接改变了这个label的宽和高,使它根据上面字符串的大小做合适的改变[label sizeToFit];NSLog(@"width=%.1f height=%.1f ", label.frame.size.width, label.frame.size.height);[self.view addSubview:label];
sizeThatFits 的例子
UILabel*label = [[UILabelalloc] initWithFrame:CGRectMake(10,10,0,0)];[label setBackgroundColor:[UIColorgrayColor]];[label setFont:[UIFontsystemFontOfSize:20]];label.text =@"没有什么工作是加工资解决不了的,如果有,那就表示工资没到位";//sizeThatFits并没有改变原始label的大小CGSizesizeThatFits = [label sizeThatFits:CGSizeZero];NSLog(@"sizeThatFits: width=%.1f height=%.1f", sizeThatFits.width, sizeThatFits.height);NSLog(@"width=%.1f height=%.1f", label.frame.size.width, label.frame.size.height);[self.view addSubview:label];
ps:自己在写跑马灯的时候使用了sizeThatFits,顺便查了查sizeToFit,做一下笔记,大神勿喷!!!