我的开发笔记---UILabel根据字符串来自适应大小
下面是更新的UIlabel使用小技巧:根据字符串的大小来定义label的大小。
通常我们都是先指定label的大小在将字符串写入label,这样往往会浪费一些空间,或空间不够不能将字符串完全显示,或字符串自适应缩小,或换行。当多个label在一起显示时就会出现一些想不到的麻烦。
如上图:label的位置都是预先指定好的,无发根据数据进行自适应位置大小,使得label间存在间隙(客户不要)。
代码:
NSString *label7String = [labelDic objectForKey:@"v7"]; //获取数据
if(label7String){
UILabel *label7 = [[UILabel alloc] initWithFrame:CGRectMake(17, 63, 60, 20)];//指定label位置大小
label7.backgroundColor = [UIColorclearColor];
label7.lineBreakMode = UILineBreakModeWordWrap;
label7.numberOfLines = 1;
label7.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
label7.text = label7String;
label7.textColor = [UIColor grayColor];
label7.textAlignment = UITextAlignmentLeft;
[button addSubview:label7];
[label7 release];
}
NSString *label8String = [labelDic objectForKey:@"v8"];
if(label8String){
UILabel *label8 = [[UILabel alloc] initWithFrame:CGRectMake(77, 60, 40, 25)];
label8.backgroundColor = [UIColorclearColor];
label8.lineBreakMode = UILineBreakModeWordWrap;
label8.numberOfLines = 1;
label8.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
label8.text = label8String;
label8.textColor = [UIColor blueColor];
label8.textAlignment = UITextAlignmentLeft;
[button addSubview:label8];
[label8 release];
}
下面的一个接口可以返回指定大小的字符串在显示时的实际大小。
CGSize titleSize = [label8String sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 30)];
如上图,label间无间隙。
代码:
CGSize size7 = CGSizeMake(0, 0); //初始化size7
NSString *label7String = [labelDic objectForKey:@"v7"]; //获取数据
if(label7String){
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:12]; //指定字符串的大小
CGSize titleSize = [label7String sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 30)]; //获取字符串的实际大小
size7 = titleSize;//保存字符串的大小(也就是label的大小)用来指定相邻的label8的位置
UILabel *label7 = [[UILabel alloc] init];
label7.frame = CGRectMake(17, 63, titleSize.width, titleSize.height);//初始化label7
label7.backgroundColor = [UIColorclearColor];
label7.lineBreakMode = UILineBreakModeWordWrap;
label7.numberOfLines = 1;
label7.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
label7.text = label7String;
label7.textColor = [UIColor grayColor];
label7.textAlignment = UITextAlignmentLeft;
[button addSubview:label7];
[label7 release];
}
CGSize size8 = CGSizeMake(0, 0); //初始化size8
NSString *label8String = [labelDic objectForKey:@"v8"];//获取数据
if(label8String){
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
CGSize titleSize = [label8String sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 30)]; //获取字符串的实际大小
size8 = titleSize;//保存字符串(label)大小,用来指定相邻的label位置
UILabel *label8 = [[UILabel alloc] init];
label8.frame = CGRectMake(17 + size7.width, 60, titleSize.width, titleSize.height);//这里根据相邻label7的位置大小来指定label8的位置,根据字符串的大小来指定label8的大小
label8.backgroundColor = [UIColor clearColor];
label8.lineBreakMode = UILineBreakModeWordWrap;
label8.numberOfLines = 1;
label8.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
label8.text = label8String;
label8.textColor = [UIColor blueColor];
label8.textAlignment = UITextAlignmentLeft;
[button addSubview:label8];
[label8 release];
}