2013-01-02 27 views
2

我必須將兩個UILabels(州,郵政編碼)並排即加利福尼亞州32320中的UITableView的cell.I可以清晰地看到,當字體大小是less.When我增加字體大小我couldnot看狀態標籤和郵政編碼標籤越來越added.This的最後一個字母是我米工作的代碼:UILabels完整文本不可見

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     static NSString *[email protected]"Cell"; 

     UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if(cell == nil) 
     { 
      cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease]; 

      state=[[UILabel alloc] initWithFrame:CGRectZero]; 
      state.tag = 116; 
      state.backgroundColor = [UIColor clearColor]; 
      [state setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]]; 
      [state setLineBreakMode:UILineBreakModeWordWrap]; 
      [cell addSubview:state]; 


      zipCode=[[UILabel alloc] initWithFrame:CGRectZero]; 
      zipCode.tag = 121; 
      zipCode.backgroundColor = [UIColor clearColor]; 
      [zipCode setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]]; 
      [zipCode setLineBreakMode:UILineBreakModeWordWrap]; 
      [cell addSubview:zipCode]; 

} 

     NSString *state1=[d objectForKey:@"State"]; 
     CGSize constraint6=CGSizeMake(175,2000.0f); 
     CGSize size6=[state1 sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint6 lineBreakMode:UILineBreakModeWordWrap]; 
     state=(UILabel *)[cell viewWithTag:116]; 
     state.frame=CGRectMake(105, city.frame.size.height+city.frame.origin.y+5, size6.width, 10); 
     [state setTextAlignment:UITextAlignmentLeft]; 

     [state setText:[d valueForKey:@"State"]]; 

     NSString *zip = [d objectForKey:@"Zip"]; 

     if([zip isEqualToString:@""]) 
     { 

      zipCode=[[UILabel alloc]initWithFrame:CGRectMake(105, 125, 320,10)]; 
      zipCode .font=[UIFont fontWithName:@"Helvetica" size:12]; 
      [zipCode setTextAlignment:UITextAlignmentLeft]; 
      zipCode.hidden=YES; 
      [zipCode release];    


     } 
     else 
     { 

      NSString *zip=[d objectForKey:@"Zip"]; 
      CGSize constraint200=CGSizeMake(175,2000.0f); 
      CGSize size200=[zip sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12] constrainedToSize:constraint200 lineBreakMode:UILineBreakModeWordWrap]; 
      zipCode=(UILabel *)[cell viewWithTag:121]; 
      zipCode.frame=CGRectMake(13+state.frame.size.width+state.frame.origin.x, city.frame.size.height+city.frame.origin.y+5, size200.width,10); 

      [zipCode setTextAlignment:UITextAlignmentLeft]; 
      [zipCode setText:[d valueForKey:@"Zip"]]; 
      zipCode.numberOfLines=0; 



     } 

return cell; 
} 

現在我可以看到的結果 加利福尼亞州32320

但是,當我減少字體 - 大小10,然後我可以清楚地看到他們(加州32320),但我需要的字體大小爲12只按我的要求。

我哪裏錯了?

回答

3

使用此代碼它將會看到符合您標籤尺寸的標籤

label.adjustsFontSizeToFitWidth = YES; 
2

請更新一些什麼大的標籤寬度。並將Lable的「setNoOfLine」屬性設置爲2.這樣,它將顯示2行文本。

希望它對你有幫助。

乾杯!

0

state標籤的字體是Helvetica-Bold,但在您的sizeWithFont中,您指定的字體是Helvetica。或者,您可以指定state.font

0

做這樣的,

CGFloat lLabelWidth = [yourText sizeWithFont: factLabel.font].width; 

CGRect _tempFrame=yourLabel.frame; 
_tempFrame.size.width=lLabelWidth; 
yourLabel.frame=_tempFrame; 

希望它會幫助你....

1

有一個小竅門,通過該標籤會根據您的文字自動調整

UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,200,40)];//set your frame 
testLabel.numberOfLines = 0;//Important to add this line 
testLabel.font = [UIFont boldSystemFontOfSize:15];//Set your font 
testLabel.text = @"Set your text here!"; 
[testLabel sizeToFit]; // this line will do the trick :)