ios開發(fā)點滴-UILable 根據(jù)文字內(nèi)容進行大小設(shè)置 sizeThatFits和sizeToFit

UIView方法之SizeToFit

作用: 計算出最優(yōu)size, 并且改變UIView的size

Demo1: 高度不變,寬度隨文本大小變化而變化

設(shè)置字號為13,使用SizeToFit自適應(yīng)結(jié)果為

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打印結(jié)果為:

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打印結(jié)果為:

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打印結(jié)果為:

frame = {{20, 100}, {544, 16}}

此時三個控件都是單行模式下,高度不變,寬度隨文本大小變化而變化.

但UILabel是有設(shè)置多行功能的,所以UILabel和UIButton均可設(shè)置多行,請看Demo2

Demo2: 寬度不變,高度隨文本大小變化而變化

設(shè)置寬度為100,UILabel的numberOfLines大于1行,假如為3行

若自適應(yīng)后的寬度小于100,則寬度小于100,高為一行

若是自適應(yīng)后的寬度大于100,則寬度為100,若高度大于3行,則高度為3行,剩余內(nèi)容不顯示,若高度小于三行,則高度為計算出的高度

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打印結(jié)果為:

frame = {{20, 100}, {97, 47}}

Demo3: 寬度不變,高度隨文本大小變化而變化

設(shè)置寬度為100,UILabel的numberOfLines為0行

若自適應(yīng)后的寬度小于100,則寬度小于100,高為一行

若是自適應(yīng)后的寬度大于100,則寬度為100,高度為自適應(yīng)后的高度

注意:高度自適應(yīng)時,寬度不可設(shè)置為0,若為0,則變成了單行寬度自適應(yīng)

但剛剛自適應(yīng)算出來的坐標,上下左右均緊貼著Label的邊,不美觀,可做如下修飾

label.width +=10;label.height +=10;

UIView方法之sizeThatFits

作用: 計算出最優(yōu)size, 但是不會改變UIView的size

用法: 將sizeThatFits的寬高設(shè)置較大點,會在這個范圍內(nèi)自動計算出最匹配寬高

若計算的寬度小于設(shè)置的寬度,則以計算出來的高度為準

若計算的寬度大于設(shè)置的寬度,則以設(shè)置的寬度去進行高度自適應(yīng)

注意:若要自適應(yīng),要重設(shè)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打印結(jié)果為:(因為有換行,所以計算出)

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 =@"

天青色等煙雨,而我在等你,炊煙裊裊升起,隔江千萬里,在瓶底書漢隸仿前朝的飄逸,就當我為遇見你伏筆,天青色等煙雨,而我在等你,月色被打撈起,暈開了結(jié)局,如傳世的青花瓷自顧自美麗,你眼帶笑意,色白花青的錦鯉躍然於碗底,臨摹宋體落款時卻惦記著你,你隱藏在窯燒里千年的秘密,極細膩猶如繡花針落地,簾外芭蕉惹驟雨,門環(huán)惹銅綠,而我路過那江南小鎮(zhèn)惹了你,在潑墨山水畫里,你從墨色深處被隱去

";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.itdecent.cn/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)];? ? //文本文字自適應(yīng)大小? ? 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];



自適應(yīng)大小ios7以后有兩種可行的方案:

1.sizeThatFits

NSString *str=@"目前支持以下站點";? ? UILabel *notice=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];? ? //文本文字自適應(yīng)大小? ? 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)];? ? //文本文字自適應(yīng)大小? ? 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大小的時候需要先進行l(wèi)able的text賦值

2.如果要將lable居中顯示的話,lable.center屬性的設(shè)置必須放在設(shè)置新大小之后,不然會出現(xiàn)不居中的情況

3.ios7之前還有其他的方法

cgSize=[str sizeWithFont:font];

這個方法是NSString的方法,聽說在ios7下使用會計算不準確

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容