UILabels完整文本不可见

时间:2013-01-02 07:12:56

标签: iphone uilabel

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

-(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。

我哪里出错?

5 个答案:

答案 0 :(得分:3)

使用此代码,它会看到标签符合标签尺寸

label.adjustsFontSizeToFitWidth = YES;

答案 1 :(得分:2)

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

希望它对你有所帮助。

干杯!

答案 2 :(得分: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 :)

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这样做,

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

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

希望它会帮助你......