UIView方法之SizeToFit
作用: 計算出最優size, 并且改變UIView的size
Demo1: 高度不變,寬度隨文本大小變化而變化
設置字號為13,使用SizeToFit自適應結果為
UILabel*label = [[UILabelalloc] initWithFrame:CGRectMake(20,100,100,40)];? ? label.backgroundColor = [UIColorwhiteColor];? ? label.textAlignment =NSTextAlignmentCenter;? ? label.font = [UIFontsystemFontOfSize:13];? ? label.text =@"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里";? ? [self.view addSubview:label];? ? [label sizeToFit];NSLog(@"frame = %@",NSStringFromCGRect(label.frame));
UILabel打印結果為:
frame = {{20, 100}, {307, 16}}
UIButton*btn = [[UIButtonalloc] initWithFrame:CGRectMake(20,100,100,40)];? ? btn.backgroundColor = [UIColorwhiteColor];? ? [btn setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];? ? btn.titleLabel.font = [UIFontsystemFontOfSize:13];? ? [btn setTitle:@"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里"forState:UIControlStateNormal];? ? [self.view addSubview:btn];? ? [btn sizeToFit];NSLog(@"frame = %@",NSStringFromCGRect(btn.frame));
UIButton打印結果為:
frame = {{20, 100}, {540, 28}}
UITextField*field = [[UITextFieldalloc] initWithFrame:CGRectMake(20,100,100,40)];? ? field.backgroundColor = [UIColorwhiteColor];? ? field.text =@"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里";? ? field.font = [UIFontsystemFontOfSize:13];? ? [self.view addSubview:field];? ? [field sizeToFit];NSLog(@"frame = %@",NSStringFromCGRect(field.frame));
UITextField打印結果為:
frame = {{20, 100}, {544, 16}}
此時三個控件都是單行模式下,高度不變,寬度隨文本大小變化而變化.
但UILabel是有設置多行功能的,所以UILabel和UIButton均可設置多行,請看Demo2
Demo2: 寬度不變,高度隨文本大小變化而變化
設置寬度為100,UILabel的numberOfLines大于1行,假如為3行
若自適應后的寬度小于100,則寬度小于100,高為一行
若是自適應后的寬度大于100,則寬度為100,若高度大于3行,則高度為3行,剩余內容不顯示,若高度小于三行,則高度為計算出的高度
UILabel*label = [[UILabelalloc] initWithFrame:CGRectMake(20,100,100,40)];? ? label.textAlignment =NSTextAlignmentCenter;? ? label.backgroundColor = [UIColorwhiteColor];? ? label.font = [UIFontsystemFontOfSize:13];? ? label.numberOfLines =3;? ? label.text =@"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里";? ? [self.view addSubview:label];? ? [label sizeToFit];NSLog(@"frame = %@",NSStringFromCGRect(label.frame));
UIlabel打印結果為:
frame = {{20, 100}, {97, 47}}
Demo3: 寬度不變,高度隨文本大小變化而變化
設置寬度為100,UILabel的numberOfLines為0行
若自適應后的寬度小于100,則寬度小于100,高為一行
若是自適應后的寬度大于100,則寬度為100,高度為自適應后的高度
注意:高度自適應時,寬度不可設置為0,若為0,則變成了單行寬度自適應
但剛剛自適應算出來的坐標,上下左右均緊貼著Label的邊,不美觀,可做如下修飾
label.width +=10;label.height +=10;
UIView方法之sizeThatFits
作用: 計算出最優size, 但是不會改變UIView的size
用法: 將sizeThatFits的寬高設置較大點,會在這個范圍內自動計算出最匹配寬高
若計算的寬度小于設置的寬度,則以計算出來的高度為準
若計算的寬度大于設置的寬度,則以設置的寬度去進行高度自適應
注意:若要自適應,要重設Label坐標
UILabel*label = [[UILabelalloc] initWithFrame:CGRectMake(20,100,100,20)];? ? label.backgroundColor = [UIColorwhiteColor];? ? label.textAlignment =NSTextAlignmentCenter;? ? label.font = [UIFontsystemFontOfSize:13];? ? label.numberOfLines =0;? ? label.text =@"天青色等煙雨\n而我在等你\n炊煙裊裊升起\n隔江千萬里";? ? [self.view addSubview:label];CGSizesizeThat = [label sizeThatFits:CGSizeMake(1000,1000)];? ? label.frame =CGRectMake(20,100, sizeThat.width, sizeThat.height);NSLog(@"frame = %@",NSStringFromCGSize(sizeThat));
UIlabel打印結果為:(因為有換行,所以計算出)
frame = {80, 62.5}
NSString方法之boundingRectWithSize
_boundingRectWithSizeLabel.numberOfLines =0;? ? _boundingRectWithSizeLabel.font = [UIFontsystemFontOfSize:18];NSString*strText =@"這是一段很長的文字,你需要計算這個高度到底是多少";? ? _boundingRectWithSizeLabel.text = strText;NSMutableDictionary*dic = [NSMutableDictionarydictionaryWithObject:[UIFontsystemFontOfSize:18] forKey:NSFontAttributeName];CGSizesize = [strText boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOriginattributes:dic context:nil].size;? ? _boundingRectWithSizeLabel.frame =CGRectMake(15, _h5TextView.bottom +30, size.width, size.height);
NSAttributedString方法之boundingRectWithSize
_boundingRectWithSizeAttributedLabel.numberOfLines =0;? ? _boundingRectWithSizeAttributedLabel.font = [UIFontsystemFontOfSize:13];NSString*h5boundingRectWithSizeString =@"
天青色等煙雨,而我在等你,炊煙裊裊升起,隔江千萬里,在瓶底書漢隸仿前朝的飄逸,就當我為遇見你伏筆,天青色等煙雨,而我在等你,月色被打撈起,暈開了結局,如傳世的青花瓷自顧自美麗,你眼帶笑意,色白花青的錦鯉躍然於碗底,臨摹宋體落款時卻惦記著你,你隱藏在窯燒里千年的秘密,極細膩猶如繡花針落地,簾外芭蕉惹驟雨,門環惹銅綠,而我路過那江南小鎮惹了你,在潑墨山水畫里,你從墨色深處被隱去
";NSMutableAttributedString*h5boundingRectWithSizeAttributedString = [[NSMutableAttributedStringalloc] initWithData:[h5boundingRectWithSizeString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nilerror:nil];? ? _boundingRectWithSizeAttributedLabel.attributedText = h5boundingRectWithSizeAttributedString;CGSizesize1 = [h5boundingRectWithSizeAttributedString boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeadingcontext:nil].size;? ? _boundingRectWithSizeAttributedLabel.frame =CGRectMake(15, _boundingRectWithSizeLabel.bottom +30, size1.width, size1.height);
來自:http://www.lxweimin.com/p/ce26f05cd7cc
http://www.ithao123.cn/content-2079099.html
1.定義一個UILable
self.view.backgroundColor =[UIColor whiteColor];? ? NSString *str=@"目前支持以下站點";? ? UILabel *notice=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];? ? //文本文字自適應大小? ? notice.adjustsFontSizeToFitWidth = YES;? ? notice.text=str;? ? notice.textAlignment=NSTextAlignmentCenter;? ? CGSize sizeThatFit=[notice sizeThatFits:CGSizeZero];? ? ? notice.center = CGPointMake(self.view.bounds.size.width/2, 20) ;? ? notice.textColor=[UIColor whiteColor];? ? notice.backgroundColor=[UIColor blackColor];? ? [self.view addSubview:notice];
自適應大小ios7以后有兩種可行的方案:
1.sizeThatFits
NSString *str=@"目前支持以下站點";? ? UILabel *notice=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];? ? //文本文字自適應大小? ? notice.adjustsFontSizeToFitWidth = YES;? ? notice.text=str;? ? notice.textAlignment=NSTextAlignmentCenter;? ? //使用sizeThatFit計算lable大小? ? CGSize sizeThatFit=[notice sizeThatFits:CGSizeZero];? ? //重新指定frame? ? notice.frame=CGRectMake(0, 0, sizeThatFit.width, sizeThatFit.height);? ? notice.center = CGPointMake(self.view.bounds.size.width/2, kL20) ;? ? notice.textColor=[UIColor whiteColor];? ? notice.backgroundColor=[UIColor blackColor];? ? [self.view addSubview:notice];
2.sizeToFit
NSString *str=@"目前支持以下站點";? ? UILabel *notice=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];? ? //文本文字自適應大小? ? notice.adjustsFontSizeToFitWidth = YES;? ? notice.text=str;? ? notice.textAlignment=NSTextAlignmentCenter;? ? [notice sizeToFit];//使用sizeToFit? ? notice.center = CGPointMake(self.view.bounds.size.width/2, kL20) ;? ? notice.textColor=[UIColor whiteColor];? ? notice.backgroundColor=[UIColor blackColor];? ? [self.view addSubview:notice];
注意:1.計算lable大小的時候需要先進行lable的text賦值
2.如果要將lable居中顯示的話,lable.center屬性的設置必須放在設置新大小之后,不然會出現不居中的情況
3.ios7之前還有其他的方法
cgSize=[str sizeWithFont:font];
這個方法是NSString的方法,聽說在ios7下使用會計算不準確