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.jianshu.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下使用会计算不准确