2

我必须在 UITableView 的单元格中并排放置两个 UILabel(州,邮编),即加利福尼亚州 32320。当字体大小较小时,我可以清楚地看到它们。当我增加字体大小时,我看不到状态标签和邮政编码的最后一个字母标签正在添加。这是我正在处理的代码:

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

        static NSString *CellIdentifier=@"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;
}

现在我可以看到结果为 Californi 32320

但是当我将字体大小减小到 10 时,我可以清楚地看到它们(加利福尼亚州 32320)。但我只需要按照我的要求将字体大小设为 12。

我要去哪里错了?

4

5 回答 5

3

使用此代码,它将看到适合您标签大小的标签

label.adjustsFontSizeToFitWidth = YES;
于 2013-01-02T07:27:10.573 回答
2

请用一些大的更新标签宽度。并使用 Lable 的“setNoOfLine”属性为 2。这样,它将以 2 行显示文本。

希望对您有所帮助。

干杯!

于 2013-01-02T07:17:03.447 回答
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 :)
于 2013-01-02T07:26:38.613 回答
0

state标签的字体是Helvetica-Bold,但在您的 中sizeWithFont,您指定的字体是Helvetica。或者,您可以只指定state.font.

于 2013-01-02T07:25:44.640 回答
0

这样做,

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

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

希望它会帮助你....

于 2013-01-02T07:25:45.867 回答