我必须显示一些文本(动态),它包含三个字符串变量,如下所示:
SanFransisco,California 32122,即city,state zipCode
所以想到用三个标签显示它们。但是我无法理解何时使用sizetoFit以及何时不使用。
如果它是一个像BrightWood Park,District of Columbia 32123
那样的大文本,那么我就像BrightWood Park,District of Columb
那样。我无法在模拟器上看到zipCode部分。所以无论文本是什么,都应该在模拟器中显示。
如果城市的文字很大,如Massachusetts Avenue Heights,District of Columbia 32123
,那么它应该显示为Massachusetts Avenue Heights,District of
Columbia 32123
目前我正在Massachusetts Avenue Heights,District of Co
NSString *city=[NSString stringWithFormat:@"%@,",self.city];
CGSize constraint4 = CGSizeMake(250, 2000.0f);
CGSize size4=[city sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
lblCity=[[UILabel alloc] init];
[lblCity setFrame:CGRectMake(60,Lane1.frame.size.height+Lane1.frame.origin.y,size4.width,size4.height) ];
lblCity.textAlignment=UITextAlignmentLeft;
lblCity.backgroundColor=[UIColor clearColor];
lblCity.text=[NSString stringWithFormat:@"%@",city];
[lblCity setNumberOfLines:0];
lblCity.highlightedTextColor=[UIColor greenColor];
[lblCity setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
[testscroll addSubview: lblCity];
NSString *state=[NSString stringWithFormat:@"%@ ",self.state];
CGSize constraint5 = CGSizeMake(250, 2000.0f);
CGSize size5=[state sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
lblState=[[UILabel alloc] init];
[lblState setFrame:CGRectMake(lblCity.frame.origin.x+lblCity.frame.size.width,Lane1.frame.size.height+Lane1.frame.origin.y,size5.width,size5.height) ];
lblState.textAlignment=UITextAlignmentLeft;
lblState.backgroundColor=[UIColor clearColor];
lblState.text=[NSString stringWithFormat:@"%@" ,state];
[lblState setNumberOfLines:0];
lblState.highlightedTextColor=[UIColor greenColor];
[lblState setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
[testscroll addSubview: lblState];
NSString *zip=[NSString stringWithFormat:@"%@",self.zip];
CGSize constraint200=CGSizeMake(250,2000.0f);
CGSize size200=[zip sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]constrainedToSize:constraint200 lineBreakMode:UILineBreakModeWordWrap ];
zipCode=[[UILabel alloc] init];
[zipCode setFrame:CGRectMake(lblState.frame.origin.x+lblState.frame.size.width,Lane1.frame.size.height+Lane1.frame.origin.y,size200.width,size200.height) ];
zipCode.textAlignment=UITextAlignmentLeft;
zipCode.backgroundColor=[UIColor clearColor];
zipCode.text=[NSString stringWithFormat:@"%@" ,zip];
[zipCode setNumberOfLines:0];
zipCode.highlightedTextColor=[UIColor greenColor];
[zipCode setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
[testscroll addSubview:zipCode];
2.何时沿numberofLines = 0使用sizetoFit,何时不使用?
答案 0 :(得分:0)
制作自动调整UILabel
有几个要点。
首先,您必须将标签的高度设置为零
第二,您必须将标签“numberOfLines
设置为零
最后,您必须致电sizeToFit
方法
像这样:
label.frame = CGRectMake(x, y, width, 0.0f);
label.numberOfLines = 0;
[label sizeToFit];
答案 1 :(得分:0)
获取字符串后,像这样设置UILabel
大小
lblCity.text=[NSString stringWithFormat:@"%@",city];
[lblCity setNumberOfLines:0];
[lblCity setFrame:CGRectMake(60,Lane1.frame.size.height+Lane1.frame.origin.y,size4.width,size4.height) ];
[lblCity sizeToFit]; it may be work
///这里我测试工作正常并显示全文
NSString *city=@"Orlando ";
CGSize constraint4 = CGSizeMake(250, 2000.0f);
CGSize size4=[city sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"size4.width %f height %f ",size4.width,size4.height);
UILabel* lblCity=[[UILabel alloc] init];
[lblCity setFrame:CGRectMake(160,16,size4.width,size4.height) ];
lblCity.textAlignment=UITextAlignmentLeft;
lblCity.backgroundColor=[UIColor clearColor];
lblCity.text=[NSString stringWithFormat:@"%@",city];
[lblCity setNumberOfLines:0];
lblCity.highlightedTextColor=[UIColor greenColor];
[lblCity setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
[lblCity sizeToFit];
[self.view addSubview: lblCity];