-1

我必须显示一些文本(动态),它们位于三个字符串变量中,如下所示:

SanFransisco,California 32122 ie city,state 邮政编码

所以想在三个标签中显示它们。但我不明白什么时候使用 sizetoFit 什么时候不使用。如果它是一个大文本,BrightWood Park,District of Columbia 32123那么我得到它就像BrightWood Park,District of Columb。我在模拟器上看不到邮政编码部分。所以无论文本可能是什么,它都应该显示在模拟器中。

如果城市的文字很大,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.什么时候使用sizetoFit沿着numberofLines=0,什么时候不使用?

4

2 回答 2

0

进行 autofit 有几个要点UILabel

  • 首先,您必须将标签的高度设置为零

  • 其次,您也必须将 label'numberOfLines设置为零

  • 最后你必须调用sizeToFit方法

像这样:

label.frame = CGRectMake(x, y, width, 0.0f);
label.numberOfLines = 0;
[label sizeToFit];
于 2013-01-28T15:45:17.717 回答
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];
于 2013-01-29T05:00:26.403 回答